使用权重平均分配嵌套布局

问题描述

我正在尝试平均分配2 RelativeLayout,它是Linear Layout的子级。
但是,只要使Weight =“ 1”和width =“ 0”即可。只是缩成一条线。
我想要的是:水平分布均匀
问题:缩小为一行

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"                     <!-- here is the problem -->
            android:layout_below="@id/commentry_text_view"
            android:id="@+id/score_and_life"
            android:paddingTop="16dp"
            android:orientation="horizontal">
        <RelativeLayout
            android:layout_weight="1dp"               <!-- Here Is weight -->
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score"
            style="@style/Header_Text"
            android:id="@+id/score_text_view"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/socreDigit"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"
            style="@style/secondary_text"
            android:id="@+id/score_Digit_Text_View"
            android:layout_toRightOf="@id/score_text_view"/>
        </RelativeLayout>

            <RelativeLayout
                android:layout_weight="1dp"                    <!-- Here Is weight -->
                 android:layout_marginLeft="150dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/life"
                style="@style/Header_Text"
                android:id="@+id/life_text_view"
                />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lifeDigit"
                    android:layout_toRightOf="@id/life_text_view"
                    android:layout_marginTop="3dp"
                    android:layout_marginLeft="3dp"
                    style="@style/secondary_text"
                    android:id="@+id/Life_count_Text_view"/>
            </RelativeLayout>
        </LinearLayout>

解决方法

将它们都设置为layout_weight =“ 1”,而不是 1dp 。权重值是一个分数,1dp是一个以像素为单位的度量,因此它很困惑并将其计数为零

通过这种方式,您可以删除weightSum="2"(这意味着您的整个宽度的权重之和为2),并且只需将每个权重设置为0.5,即宽度的50%。哪个对您最有意义!

,

我已经解决了您的问题,方法是将两种相对布局中的android:layout_weight="1dp"更改为android:layout_weight="1",并消除两种布局中不必要的边距。请检查以下可解决您问题的XML修复程序。

<?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="wrap_content"
    android:weightSum="2"
    android:id="@+id/score_and_life"
    android:paddingTop="16dp"
    android:orientation="horizontal">
    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="score"

            android:id="@+id/score_text_view"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="socreDigit"
            android:layout_marginTop="3dp"


            android:id="@+id/Score_Digit_Text_View"
            android:layout_toRightOf="@id/score_text_view"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rfymkt,"

            android:id="@+id/life_text_view"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ejnetjn"
            android:layout_toRightOf="@id/life_text_view"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"

            android:id="@+id/Life_count_Text_view"/>
    </RelativeLayout>
</LinearLayout>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...