ListView的项目布局在targetSdkVersion="17"和targetSdkVersion="18"之间存在差异

5

我刚刚将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中的工作方式相同?


1
我也遇到了View高度的同样问题。我不知道该如何解决。我将继续使用API级别17。 - passsy
1个回答

0

这对我来说似乎非常奇怪。据我所见,您的LinearLayout不应该需要设置layout_alignBottom/layout_alignTop属性,但是如果删除这些行,则LinearLayout本身也会显示错误。 在我的测试中,我认为能够获得您想要的结果的唯一方法是从您的LinearLayout中删除alignTop/alignBottom(因为这些似乎是不必要的),并添加以下行:

  android:minHeight="100dip"

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接