【发布时间】: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