Android的布局:TableView中 [英] Android Layouts: TableView

查看:172
本文介绍了Android的布局:TableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何使以下

<?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>

看起来更像低于目标的例子。填充似乎并不被按预期工作,我也认为这是需要集中全表的布局。有人可以帮助我了解什么错?我需要修改的匹配目标布局(3×2的3×3代替)?谢谢你。

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.

推荐答案

使用的ImageButton 而不是按钮,和使用嵌套的的LinearLayout 在每个表格单元格中正确居中每个按钮下面的文本:

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>

通知的android:layout_gravity =CENTER_HORIZONTAL这水平中心视图在父视图。这code可能需要一些改进,但它的总体思路是你应该遵循(使用layout_gravity,嵌套的布局和ImageButtons)。

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="*" >

这篇关于Android的布局:TableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆