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

android – layout_weight与它应该的相反

在下面的xml中,我试图绘制一个包含两个块的布局(LinearLayout和TextView).我希望LinearLayout比TextView大5倍.这个xml与我期望的完全相反,TextView占用的空间比LinearLayout多5倍.请注意,我已将两个元素的宽度设置为0dp,这是一个常见的疏忽.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:layout_gravity="center_vertical"
    android:weightSum="6"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="5" >

        <TextView
            android:id="@+id/result_title_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#222222"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/result_info_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#777777"
            android:textSize="12sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/result_distance_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="Arial"
        android:textColor="#ffffffff"
        android:textSize="14sp" />

</LinearLayout>

编辑:此布局实际上是列表项,包含在此列表中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>

解决方法

我也遇到了同样的问题,解决方法是改变:

>如果父LinearLayout具有orientation =“horizo​​ntal”,则子视图的宽度为0dp;>如果父LinearLayout具有orientation =“vertical”,则子视图的高度为0dp.

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

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

相关推荐