我刚刚将Android SDK更新到版本18,并修改了我正在进行的项目,使其使用它而不是版本17。事实证明,我的ListView现在看起来大不相同了。然而,只要在清单文件中将targetSdkVersion从18切换到17,它就可以再次正确。
我在Eclipse中创建了一个新的Android项目,并将主要活动更改为最基本的ListActivity实现,从而成功地再现了这个问题:
public class MainActivity extends ListActivity {
private static final String[] listItems = new String[] { "list item 1", "list item 2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, listItems));
}
}
list_item.xml文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="100dip"
android:background="#ff0000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/text"
android:layout_alignTop="@id/text"
android:background="#88ffffff"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#8c0000ff"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dip"
android:background="#8c00ff00"
android:text="@string/button2" />
</LinearLayout>
</RelativeLayout>
有意将LinearLayout置于TextView之上。我想使用LinearLayout作为覆盖,并在必要时显示/隐藏它。
现在,当我将AndroidManifest.xml文件中的targetSdkVersion设置为17时,一切都按预期进行,这意味着按钮与LinearLayout的高度相匹配。然而,当我将版本切换到18时,它们的行为就像使用了"wrap_content"。为什么我会出现这种奇怪的行为,以及如何将其修复为在SDK 17中工作?
这对我来说似乎很奇怪。据我所见,你的LinearLayout也不需要设置layout_alignBottom/layout_alignTop属性,但删除这些行,LinearLayout本身也会错误显示。我能得到我认为是你在我的测试中想要的结果的唯一方法是从你的LinearLayout中删除alignTop/alignBottom(因为这些似乎没有必要),并添加行:
android:minHeight="100dip"
希望这能有所帮助。