Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 布局图\u重量与应有重量相反_Android_Android Linearlayout_Android Layout Weight - Fatal编程技术网

Android 布局图\u重量与应有重量相反

Android 布局图\u重量与应有重量相反,android,android-linearlayout,android-layout-weight,Android,Android Linearlayout,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

在下面的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>

问题来自具有“包裹内容”宽度的容器列表。更改为“匹配父项”修复了该问题

<?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="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>

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

  • 如果父
    线性布局
    具有
    orientation=“horizontal”
    ,则子视图到0dp的宽度

  • 如果父级
    LinearLayout
    具有
    orientation=“vertical”
    ,则子视图到0dp的高度


set:android:layout\u weight=“1”表示线性布局,android:layout\u weight=“5”表示文本。交换重量。是的,我知道这是有效的,但问题更多的是为什么?它应该有相反的行为。这是。。。。错。你能附上一张截图吗?另外,如果您最近更改了布局的顺序,请尝试干净的构建。我只是复制粘贴了您的代码,效果很好…线性布局显示的大小是textview的5倍。谢谢,我发现了问题,感谢您的评论。请参阅我的编辑。问题来自我的容器列表的“包装内容”宽度。如果有人能解释为什么它会产生相反的体重行为。。。否则我会回复我的帖子。这是一个惊人的发现相信我!虽然这件事没有发生的原因,但它至少让我找到了它发生的地方。我在活动的框架布局中膨胀一个片段。我的片段的比例是4:6,但它的行为类似于6:4,因为活动中的框架布局有android:Layout\u height=“wrap\u content”。更改要与之匹配的内容是成功的!这对我很有帮助,因为在我的示例中,我不想更改布局以匹配父项。非常感谢。也许这应该是正确的答案。