【发布时间】:2022-11-03 17:22:21
【问题描述】:
我有一个有两列的网格,每列都有一个标签。我希望第二列的标签向右对齐:
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
app:columnCount="2">
<TextView
android:text="Cell 0"
app:layout_column="0"
app:layout_columnWeight="1"
android:background="#FF0000" />
<TextView
android:text="Cell 1"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="right"
android:background="#0000FF"/>
</androidx.gridlayout.widget.GridLayout>
我怎样才能使第一列成为平等的到第二列,就像在下面的模型中一样?
我尝试设置android:gravity="right",但它解决了一半的问题。列的宽度相等,但蓝色标签填满了列:
<TextView
android:text="Cell 1"
app:layout_column="1"
app:layout_columnWeight="1"
android:gravity="right"
android:background="#0000FF"/>
GridLayout 计算比例大小的列的宽度的方式非常奇怪。它似乎考虑了子视图的宽度。该文档无助于描述 layout_columnWeight 的工作原理。
【问题讨论】:
-
尝试
android:gravity="right"而不是app:layout_gravity="right" -
@Omkar 谢谢,但它不会产生我期望的结果。两列相等,但蓝色
TextView水平填充列。我需要它保持与右侧对齐。
标签: java android android-gridlayout