ListView item layout differs between targetSdkVersion=“17” and targetSdkVersion=“18”

a 夏天 提交于 2019-12-23 10:07:46

问题


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.

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));
    }

}

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>

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

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?


回答1:


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"

Hope this helps.



来源:https://stackoverflow.com/questions/17993415/listview-item-layout-differs-between-targetsdkversion-17-and-targetsdkversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!