开发者

Android TableLayout weights are not exact

In the layout below I expect the rows to have exact heights first row 178px and the next two of 71px. Instea开发者_运维问答d 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> 


Your weights are being used to distribute the extra space, not all the space. Try setting android:layout_height="0px" for each TableRow; that will make the extra space 320 pixels. You can also eliminate the android:layout_width and android:layout_height attributes for the view in each TableRow (they are overridden by TableRow).

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.


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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="480px"
    android:layout_height="320px"
    android:background="#99ff0101"
    android:padding="0dp"
    android:layout_margin="0dp"
    android:weightSum="320"
    android:orientation="vertical" >
        <TextView
          android:text=""
          android:background="#99aa0000"
          android:layout_width="fill_parent"
          android:layout_height="0px"
          android:padding="0dp"
          android:layout_weight="178"
           />
        <TextView
          android:text=""
          android:background="#990000aa"
          android:layout_width="fill_parent"
          android:layout_height="0px"
          android:padding="0dp"
          android:layout_margin="0dp"
          android:layout_weight="71"  />

        <TextView
          android:text=""
          android:background="#9900aa00"
          android:layout_width="fill_parent"
          android:layout_height="0px"
          android:padding="0dp"
          android:layout_margin="0dp"
          android:layout_weight="71" 
          />
</LinearLayout >

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜