17 QUOT; ListView项布局targetSdkVersion = QUOT之间是不同的;和targetSdkVersion = QUOT; 18';布局、不同、ListView、Q

2023-09-07 23:13:33 作者:妳並不難懂\

我刚刚更新了Android SDK到版本18,修改我正在上使用它,而不是17版的事实证明,我的ListView看起来很大的不同,现在该项目。然而,仅仅从18切换targetSdkVersion到17内的manifest文件使得它便又。

I just updated the Android SDK to version 18 and modified the project I'm working on to use it instead of version 17. It turns out that my ListView looks much different now. However, just switching targetSdkVersion from 18 to 17 inside the manifest file makes it right again.

我设法通过创建Eclipse的一个新的Andr​​oid项目和改变的主要活动最基本的ListActivity执行可能重现该问题:

I managed to reproduce the problem by creating a new Android project in Eclipse and changing the main activity to the most basic ListActivity implementation possible:

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文件包含以下内容:

The list_item.xml file contains the following:

<?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作为覆盖和显示/在必要时将其隐藏。

Having the LinearLayout over the TextView is intentional. I want to use the LinearLayout as an overlay and show/hide it when necessary.

现在,当我设置targetSdkVersion AndroidManifest.xml文件里面17,一切正常,这意味着按钮匹配的LinearLayout的高度。然而,当我的版本切换到18岁,他们表现得好像他们使用WRAP_CONTENT。为什么会出现这种奇怪的行为,我怎么能解决它在17 SDK工作?

Now, when I set the targetSdkVersion inside the AndroidManifest.xml file to 17, everything works as expected, meaning that the buttons match the LinearLayout's height. However, when I switch the version to 18, they behave as if they used "wrap_content". Why am I getting this strange behavior and how can I fix it to work as in SDK 17?

推荐答案

这似乎很奇怪,我。你的LinearLayout不应该需要layout_alignBottom / layout_alignTop属性之一,据我可以看到设置,但除去这些线和的LinearLayout本身显示错误了。我能得到什么,我相信的唯一方法是,你在我的测试想要的结果是从LinearLayout中删除alignTop / alignBottom(因为这些似乎是不必要的),并添加一行:

This seems very strange to me. Your LinearLayout shouldn't need the layout_alignBottom/layout_alignTop properties set either as far as I can see, but remove these lines and the LinearLayout itself displays wrongly too. The only way I could get what I believe is the result you want in my testing was to delete alignTop/alignBottom (as these seem unnecessary) from your LinearLayout and add the line:

  android:minHeight="100dip"

希望这有助于。