Android Layouts: TableView

纵饮孤独 提交于 2019-12-25 11:45:45

问题


Can anyone tell me how to make my sample below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_height="wrap_content" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#300000"
        android:textColor="#ff0000"
        android:text="Sample Layout"/>
    </LinearLayout>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TableRow>
    <Button
        android:layout_column="1"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    <Button
        android:layout_column="2"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    <Button
        android:layout_column="3"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    </TableRow>
    <TableRow>
    <TextView
        android:layout_column="1"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:text="News Feed"/>
    <TextView
        android:layout_column="2"
        android:layout_height="wrap_content"
        android:text="Profile"
        android:padding="2dip"/>
    <TextView
        android:layout_column="3"
        android:layout_height="wrap_content"
        android:text="Friends"
        android:padding="2dip"/>
    </TableRow>
    <TableRow>
    <Button
        android:layout_column="1"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    <Button
        android:layout_column="2"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    <Button
        android:layout_column="3"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
        android:padding="2dip"/>
    </TableRow>
    <TableRow>
    <TextView
        android:layout_column="1"
        android:layout_height="wrap_content"
        android:text="Messages"
        android:padding="5dip"/>
    <TextView
        android:layout_column="2"
        android:layout_height="wrap_content"
        android:text="Places"
        android:padding="5dip"/>
    <TextView
        android:layout_column="3"
        android:layout_height="wrap_content"
        android:text="Groups"
        android:padding="5dip"/>
    </TableRow>
    </TableLayout>
</LinearLayout>

look more like the target example below. The padding doesn't seem to be working as expected and I also think it's needed to center the full table layout. Can someone help me to understand what's going wrong? And what I need to modify to match the target layout (3x2 instead of 3x3)? Thanks.


回答1:


Use ImageButton instead of Button, and use a nested LinearLayout in each table cell to center the text correctly under each button :

<TableRow>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <ImageButton
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:src="@drawable/icon" />
        <TextView
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dip"
            android:text="News Feed"/>
    </LinearLayout>

    <LinearLayout>
        ...
    </LinearLayout>
</TableRow>

Notice android:layout_gravity="center_horizontal" which centers your view horizontally in the parent view. This code may need some improvements, but it is the general idea you should follow (use layout_gravity, nested layouts, and ImageButtons).

You can also improve your table layout so it will use the whole space available :

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:stretchColumns="*" >


来源:https://stackoverflow.com/questions/8546572/android-layouts-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!