我已经彻底搜索了 google 和 stackoverflow,但我得到的唯一答案是:使用 layout Weight = "1"
设置每个 Tablerow
。这不起作用。
现在,TableLayout
中有八行,每行包含 5 个 LinearLayouts
。每个 LinearLayout 包含 2 个 TextView
,FontSizes
从 TextView
到 TextView
不同。
事实证明,TableRow 的高度有所不同,具体取决于所包含的 TextView
的字体大小。
奇怪的是,无论我将TableRows的layout_height设置为“0dp”还是“match_parent”或“fill_parent”,都不会对布局产生任何影响。
如何强制 TableRows
具有相同的高度?我不想使用特定的 pts 值作为高度,因为 tableRows 必须在它们之间平均划分整个屏幕。
这是xml的一部分,实际文件很大:
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:weightSum="8"
tools:context=".MainActivity" >
<TableRow
android:layout_width="match_parent"
android:layout_weight="1"
tools:context=".MainActivity">
<LinearLayout
android:clickable="true"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/btnmargintop"
android:layout_marginBottom="@dimen/btnmarginbot"
android:layout_marginLeft="@dimen/btnmarginleft"
android:layout_marginRight="@dimen/btnmarginright"
android:layout_weight="1"
android:background="@drawable/bg" >
<myapp.TypefacedTextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="@integer/layoutweight1"
android:layout_gravity="center"
stevens:typeface="myfont.otf"
android:clickable="false"
android:textSize="@dimen/fontsize1" />
<myapp.TypefacedTextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="@integer/layoutweight2"
android:layout_gravity="center"
stevens:typeface="myfont.otf"
android:clickable="false"
android:textSize="@dimen/fontsize2" />
</LinearLayout>
最佳答案
根据Android developer Documentation of TableLayout ,你不能这样做。
The children of a TableLayout cannot specify the
layout_width
attribute. Width is alwaysMATCH_PARENT
. However, thelayout_height
attribute can be defined by a child; default value isViewGroup.LayoutParams.WRAP_CONTENT
. If the child is a TableRow, then the height is alwaysViewGroup.LayoutParams.WRAP_CONTENT
.
但是,您可以使用垂直 LinearLayout 来实现类似的效果。
关于android:如何强制TableRows具有相同的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15137674/