【问题标题】:Android Layout Weight Sum Not Splitting ProperlyAndroid 布局权重总和未正确拆分
【发布时间】:2023-04-03 02:27:01
【问题描述】:

我的应用程序上的一个页面有一个表格布局页面,其中一行似乎无法正常工作。我的目标是在左单元格和右单元格之间平均分割页面。左边的单元格包含一个字符串,右边的单元格包含一个图像和字符串。我曾尝试使用 weightSum 来做到这一点。左边单元格的权重为 4/8,右边的单元格被放入线性布局中,其中图像的权重为 1/8,字符串的权重为 3/8。但是,在布局中,左侧单元格占据了行的大部分,大约 75%,我不知道为什么。

我在这一行下面的行几乎尝试了同样的事情,但没有遇到同样的问题。

我的 xml(为了相关性而删减):

   <TableRow
    android:layout_width="match_parent">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Class"
        android:id="@+id/classTextView"
        android:layout_marginTop="14dp"
        android:layout_weight="1"
        android:gravity="center"/>

    <LinearLayout>
        <View
            android:layout_width="4dp"
            android:gravity="left"
            android:layout_height="match_parent"
            android:background="#663300"/>

        <ImageView
            tools:ignore="ContentDescription"
            android:id="@+id/classicon"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/barbarianicon50"
            android:layout_gravity="end"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Class Value"
            android:id="@+id/classValueView"
            android:layout_marginTop="14dp"
            android:layout_weight="3"
            android:gravity="left"/>
    </LinearLayout>
</TableRow>

<View
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:background="#663300"/>




<TableRow android:layout_width="match_parent">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Strength"
        android:id="@+id/strengthTextView"
        android:layout_marginTop="20dp"
        android:layout_weight="4"
        android:gravity="center" />

    <LinearLayout>
        <View
            android:layout_width="4dp"
            android:gravity="left"
            android:layout_weight="0"
            android:layout_height="match_parent"
            android:background="#663300"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Strength Value"
            android:id="@+id/strengthValView"
            android:layout_marginTop="20dp"
            android:layout_weight="4"
            android:gravity="center" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Intelligence"
        android:id="@+id/intelligenceTextView"
        android:layout_marginTop="20dp"
        android:layout_weight="4"
        android:gravity="center" />

    <LinearLayout>
        <View
            android:layout_width="4dp"
            android:gravity="left"
            android:layout_weight="0"
            android:layout_height="match_parent"
            android:background="#663300"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Intelligence Value"
            android:id="@+id/intelligenceValView"
            android:layout_marginTop="20dp"
            android:layout_weight="4"
            android:gravity="center" />
    </LinearLayout>
</TableRow>

第一个表格行是有问题的 - 它应该在左右单元格之间平均分配,但主要是向右。

第二个表格行是它正常工作的一个示例 - 该行有 2 对单元格,但每个单元格和单元格对正确地平均分配它们之间的宽度。

我不知道为什么第一个不起作用。感谢您的帮助!

【问题讨论】:

  • I have tried to do so using weighSum 我在提供的 xml 中没有看到 weightSum?
  • WeightSum 高于此代码。我的错,我也应该发布那个。我将其定义为 android:weightSum="8" 编辑:为清楚起见,这是使用 weightSum。

标签: android android-layout android-layout-weight


【解决方案1】:

我终于成功修复了布局!

使用的方法是将&lt;include /&gt; 另一个布局作为该布局内的行元素。所有这些行元素都具有相同的数据,因此每当视图发生变化时,我都会以编程方式使用其必要的数据填充行元素。您也可以将其作为 ListView 执行,ListView 中的每一行都是另一对元素,但我不确定这对 4 个元素宽行的效果如何。

每一行的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">


<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/statNameString"
    android:id="@+id/statName"
    android:layout_marginTop="30dp"
    android:gravity="center" />

<View
    android:layout_width="4dp"
    android:gravity="left"
    android:layout_height="match_parent"
    android:background="#663300"/>
<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/statValString"
    android:id="@+id/statVal"
    android:layout_marginTop="30dp"
    android:gravity="center" />

</LinearLayout>

顶行(代表我的应用程序中的角色类)需要包含一个 ImageView,因此它的布局略有变化。 ImageView 放置在 bar View 之后,最后一个 TextView 之前。

将每一行包含在最终布局中的 XML,用于 Land 布局:

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/horizontal_class_row_layout"
        android:id="@+id/strValues"
        android:layout_column="0" />

    <include
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/horizontal_class_row_layout"
        android:id="@+id/intValues"
        android:layout_column="0" />
</TableRow>

如果您只希望每行中有一对元素,您可以简单地删除其中一个包含标签。如前所述,您也可以使用 ListView 来执行这些操作,并填充数据,我最终可能会对其进行修改。

最后,您以编程方式用 Java 中的数据填充元素,因此它们不会只说“类”和“类值”或任何默认数据:

private void fillViewValues() {
    //Gather views
    LinearLayout llClass = (LinearLayout)findViewById(R.id.classValues);
    LinearLayout llStr = (LinearLayout)findViewById(R.id.strValues);
    LinearLayout llDex = (LinearLayout)findViewById(R.id.dexValues);
    LinearLayout llCon = (LinearLayout)findViewById(R.id.conValues);
    LinearLayout llInt = (LinearLayout)findViewById(R.id.intValues);
    LinearLayout llWis = (LinearLayout)findViewById(R.id.wisValues);
    LinearLayout llCha = (LinearLayout)findViewById(R.id.chaValues);

    //Get the layout's locations
    TextView classNameView = (TextView) llClass.findViewById(R.id.statName);
    TextView classValView = (TextView) llClass.findViewById(R.id.statVal);
    ImageView classImage = (ImageView)  llClass.findViewById(R.id.classicon);

    TextView strNameView = (TextView) llStr.findViewById(R.id.statName);
    TextView strValView = (TextView) llStr.findViewById(R.id.statVal);

    TextView dexNameView = (TextView) llDex.findViewById(R.id.statName);
    TextView dexValView = (TextView) llDex.findViewById(R.id.statVal);

    TextView conNameView = (TextView) llCon.findViewById(R.id.statName);
    TextView conValView = (TextView) llCon.findViewById(R.id.statVal);

    TextView intNameView = (TextView) llInt.findViewById(R.id.statName);
    TextView intValView = (TextView) llInt.findViewById(R.id.statVal);

    TextView wisNameView = (TextView) llWis.findViewById(R.id.statName);
    TextView wisValView = (TextView) llWis.findViewById(R.id.statVal);

    TextView chaNameView = (TextView) llCha.findViewById(R.id.statName);
    TextView chaValView = (TextView) llCha.findViewById(R.id.statVal);

    //Fill those values
    //Names of sections
    classNameView.setText(getResources().getString(R.string.classString));
    strNameView.setText(getResources().getString(R.string.strString));
    dexNameView.setText(getResources().getString(R.string.dexString));
    conNameView.setText(getResources().getString(R.string.conString));
    intNameView.setText(getResources().getString(R.string.intString));
    wisNameView.setText(getResources().getString(R.string.wisString));
    chaNameView.setText(getResources().getString(R.string.chaString));

    //Values
    classValView.setText(classString);
    setImage(classImage, classString);
    strValView.setText(String.format("%d", finalStats[0]));
    dexValView.setText(String.format("%d", finalStats[1]));
    conValView.setText(String.format("%d", finalStats[2]));
    intValView.setText(String.format("%d", finalStats[3]));
    wisValView.setText(String.format("%d", finalStats[4]));
    chaValView.setText(String.format("%d", finalStats[5]));
}

这遵循at this StackOverflow post. 的建议

我要注意的一件事是,当您更改布局时,您需要将内容视图重置为当前布局。在我的例子中,函数看起来像:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        setContentView(R.layout.activity_see_character);
        fillViewValues();
    }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.activity_see_character);
        fillViewValues();
    }
}

其中 fillViewValues() 是前一个函数。我相信这会将布局设置为布局的纵向或横向版本,然后可以访问其元素。如果您在尝试访问元素时不这样做,它将找不到它们并给出 NullPointerException。

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多