如何解决sdk 17和18之间列表项布局行为的变化?

jph*_*jph 7 android android-layout android-listview android-linearlayout android-compatibility

我创建了一个简单的应用程序来说明在包含在SDK 17和SDK 18之间的RelativeLayout时LinearLayout行为的变化.首先,截图:

当targetSdkVersion为"17"时:

在此输入图像描述

当targetSdkVersion为"18"时:

在此输入图像描述

此活动的布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff"
    android:orientation="vertical" >

    <include layout="@layout/test_list_item"/>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:overScrollMode="never"
        android:scrollingCache="false" >
    </ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

text_list_item.xml是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffff00"
        android:padding="10dp"
        android:text="foobar"
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/text"
        android:layout_alignTop="@id/text"
        android:background="#00ff00"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#0000ff" />
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动的onCreate()如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    findViewById(R.id.text).bringToFront();
    final ListView mListView = (ListView) findViewById(R.id.listview);
    mListView.setAdapter(new TestAdapter());
}
Run Code Online (Sandbox Code Playgroud)

TestAdapter看起来像这样:

public class TestAdapter extends BaseAdapter {

    private static final int COUNT = 3;

    @Override
    public int getCount() {
        return COUNT;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_item, null);
        view.findViewById(R.id.text).bringToFront();
        return view;
    }
Run Code Online (Sandbox Code Playgroud)

当target = 17时,LinearLayout的子视图在ListView内部和ListView外部时,可以正确调整为可用宽度的50%.当target = 18时,当用作列表项的布局时,它不会调整大小.在这种情况下,它的宽度保持在"0dp"并且永远不会调整大小.

理想情况下,我想针对SDK 19.但是,为了做到这一点,我以某种方式适应这种行为的变化.有关如何实现这一点的任何想法?

顺便说一句,有些原因导致布局如此复杂和"怪异"; 我意识到有更直接的方法来实现这种特殊的视觉效果.

编辑:

为了更多地了解我为什么使用这种病态布局:它用于"仪表".使用ScaleAnimation拉伸或缩小LinearLayout内的视图,使其占据总可用宽度的所需百分比.让它变得棘手的是我需要在仪表顶部覆盖文本,我希望仪表的高度由文本决定.

因此,外部RelativeLayout应该"与其父级一样宽",但只"与它包含的TextView一样高".RelativeLayout中的LinearLayout应该与其父级一样宽和高.LinearLayout中的View应该与包含它的LinearLayout一样高,但只有一半宽.

当布局未用作ListView项目的内容时,这正是我在定位SDK 17 和/或 SDK 18+ 时所获得的行为.

编辑

发布在开发人员组:

https://groups.google.com/forum/#!msg/android-developers/KuoY5Tklyms/TbNq6ndD_PwJ

con*_*n_9 1

看起来因为它只是一个视图,不需要渲染任何内容,所以为了优化某些测量方法被跳过。将 View 更改为 TextView 并设置一些文本,您将看到它确实按预期显示。我确实明白 LinearLayout 以不同的方式呈现视图(不确定那是否正确或 ListView 是否正确)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#ffff00"
    android:padding="10dp"
    android:text="foobar"
    android:textColor="#000000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/text"
    android:layout_alignTop="@id/text"
    android:background="#00ff00"
    android:orientation="horizontal"
    android:weightSum="1.0" >

    <TextView
        android:id="@+id/testView"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.5"
        android:background="#0000ff"
        android:singleLine="false" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_list_item, null);

        view.findViewById(R.id.text).bringToFront();
        TextView testView = (TextView)view.findViewById(R.id.testView);
        testView.setText("some test text for viewNo:"+String.valueOf(position));

        return view;
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述