Android布局:TableView
谁能告诉我如何制作下面的样品Android布局:TableView,android,layout,gridview,padding,Android,Layout,Gridview,Padding,谁能告诉我如何制作下面的样品 <?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"&
<?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>
看起来更像下面的目标示例。填充似乎没有按预期工作,我还认为需要将整个表格布局居中。有人能帮我了解出了什么问题吗?我需要修改什么来匹配目标布局(3x2而不是3x3)?谢谢
使用
图像按钮
而不是按钮
,并在每个表格单元格中使用嵌套的线性布局
,以便在每个按钮下将文本正确居中:
<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:stretchColumns,表现在居中。非常感谢。这肯定会使情况有所改善。只是好奇,我是否因为按钮没有文本而使用ImageButtons?另外,我注意到您在文本周围添加了填充,而不是实际的ImageButton(但是ImageButtons看起来有填充)。如何删除ImageButton周围的填充?是的,如果希望显示图像而不是按钮,则ImageButtons会更好。在这里,我使按钮的背景透明(颜色#00000000),但它可以有背景和图像。我建议尝试查找类似应用程序的源代码,以了解如何继续。您将节省大量时间,因为android布局有时会非常棘手……我没有看到我在TextView周围设置了填充,但是如果您想对ImageButtons执行相同的操作,请使用边距而不是填充(android:marginLeft、marginRight等)。好的,我现在明白了。非常感谢LadaRaider!是的,我已经为这种布局寻找了一段时间的现有源代码。再次感谢!不客气!我认为这种布局(仪表盘)在谷歌上应该不难找到。
<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:stretchColumns="*" >