微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在sdk 17和18之间改变列表项布局行为的变化?

我创建了一个简单的应用程序来说明在包含在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>

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>

活动的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());
}

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

当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

解决方法

看起来因为它只是一个视图而且不需要渲染任何东西,为了一些优化,我们跳过了测量方法.将视图更改为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>
@Override
    public View getView(int position,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;
    }

原文地址:https://www.jb51.cc/android/315537.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐