我刚刚将 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"
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)