Android TableLayout 权重不准确
在下面的布局中,我希望行的高度准确,第一行为 178 像素,后两行为 71 像素。相反,我得到了底部两个带有一些额外像素的图像。我做错了什么或者表格布局没有我喜欢的那么精确?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="480px"
android:layout_height="320px"
android:background="@drawable/screen"
android:padding="0dp"
android:layout_margin="0dp"
android:stretchColumns="0"
android:weightSum="320" >
<TableRow android:layout_weight="178" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#99aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
/>
</TableRow>
<TableRow android:layout_weight="71" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#990000aa"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_margin="0dp" />
</TableRow>
<TableRow android:layout_weight="71" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#9900aa00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_margin="0dp"
/>
</TableRow>
</TableLayout>
</blink>
In the layout below I expect the rows to have exact heights first row 178px and the next two of 71px. Instead I am getting the bottom two with a few extra pixels. What am I doing wrong or is the tablelayout just not as precise as I like?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="480px"
android:layout_height="320px"
android:background="@drawable/screen"
android:padding="0dp"
android:layout_margin="0dp"
android:stretchColumns="0"
android:weightSum="320" >
<TableRow android:layout_weight="178" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#99aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
/>
</TableRow>
<TableRow android:layout_weight="71" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#990000aa"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_margin="0dp" />
</TableRow>
<TableRow android:layout_weight="71" android:padding="0dp" android:layout_margin="0dp">
<TextView
android:text=""
android:gravity="center_horizontal"
android:background="#9900aa00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_margin="0dp"
/>
</TableRow>
</TableLayout>
</blink>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的权重用于分配额外空间,而不是所有空间。尝试为每个
TableRow
设置android:layout_height="0px"
;这将使额外的空间达到 320 像素。您还可以消除每个TableRow
中视图的android:layout_width
和android:layout_height
属性(它们被TableRow< /代码>)。
顺便说一句,指定精确的像素尺寸(0 除外)通常是一个糟糕的主意;它不会缩放到不同的屏幕尺寸和/或密度。
Your weights are being used to distribute the extra space, not all the space. Try setting
android:layout_height="0px"
for eachTableRow
; that will make the extra space 320 pixels. You can also eliminate theandroid:layout_width
andandroid:layout_height
attributes for the view in eachTableRow
(they are overridden byTableRow
).By the way, it's generally a poor idea to specify exact pixel dimensions (other than 0); it doesn't scale to different screen sizes and/or densities.
TableRows 的layout_height 也没有影响它,看起来在3.0 中TableLayout 按预期工作,但旧版本很奇怪。一位朋友建议我尝试使用 LinearLayout 类似的方法,它确实有效:
至于确切的像素尺寸,我完全知道不建议这样做,我只是测试行的缩放方式并使用它作为示例来显示我的问题。
The layout_height for the TableRows did not affect it either, it seems in 3.0 the TableLayout works as expected but older versions are weird. A friend suggested I try similar approach with LinearLayout and it actually worked:
As for the exact pixel dimensions, I am fully aware its not recommended, I was just testing how the rows are scaling and using it as an example to display my problem.