android - targetSdkVersion ="17"和 targetSdkVersion ="18"ListView 项目布局不同

标签 android android-listview

我刚刚将 Android SDK 更新到版本 18 并修改了我正在处理的项目以使用它而不是版本 17。事实证明我的 ListView 现在看起来有很大不同。但是,只需将 list 文件中的 targetSdkVersion 从 18 切换到 17 即可再次正确。

我设法通过在 Eclipse 中创建一个新的 Android 项目并将主要 Activity 更改为最基本的 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"

希望这对您有所帮助。

关于android - targetSdkVersion ="17"和 targetSdkVersion ="18"ListView 项目布局不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993415/

相关文章:

android - 是否可以在 android 中使用 Actionbar sherlock 在操作菜单项中实现切换按钮

android - 适用于 Android 的纯 C++ 应用程序及其性能

android - 禁用android中的系统警报框

安卓工作室 : Drawable xml preview not working

android - 如何在 Activity 组内显示警报?

java - 如何从主 Activity 刷新 Fragment 中的 ListView?

android - 在 ViewStub 中使用 ListView 时出现 NullPointerException

java - android中如何显示已安装的应用程序?

android - CursorAdapter 动态排序

android - 如何添加取决于 ListView 中 TextView 文本的 ImageView