我刚刚更新了Android SDK到版本18,并修改了我正在做的项目,用它代替了版本17。事实证明,我的ListView现在看起来有很大的不同。但是,只需将清单文件中的targetSdkVersion从18切换到17,就可以再次实现正确。
我在Eclipse中创建了一个新的安卓项目,并将主要活动更改为最基本的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>有意在TextView上使用LinearLayout。我想使用LinearLayout作为覆盖层,并在必要时显示/隐藏它。
现在,当我将AndroidManifest.xml文件中的targetSdkVersion设置为17时,一切工作正常,这意味着按钮与LinearLayout的高度相匹配。但是,当我将版本切换到18时,它们的行为就像使用了"wrap_content“一样。为什么我会得到这种奇怪的行为,我如何才能修复它,使其像SDK 17中那样工作?
发布于 2013-08-01 21:15:35
这对我来说似乎很奇怪。据我所知,您的LinearLayout应该不需要设置layout_alignBottom/layout_alignTop属性,但是删除这些行,LinearLayout本身也会错误地显示。我想在我的测试中得到您想要的结果的唯一方法是从您的LinearLayout中删除alignTop/alignBottom (因为它们似乎是不必要的),并添加以下代码行:
android:minHeight="100dip"希望这能有所帮助。
https://stackoverflow.com/questions/17993415
复制相似问题