以下是我遇到的问题:我想在Android中创建一个类似于这个网格的布局:
为了做到这一点,我已经创建了这个布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<Button
android:id="@+id/clientimain"
android:layout_width="0dip"
android:layout_weight="1" android:layout_height="fill_parent"/>
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3" >
<Button
android:id="@+id/button2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button"/>
<Button
android:id="@+id/button6"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3" >
<Button
android:id="@+id/button3"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button"
/>
<Button
android:id="@+id/button7"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Button"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
所以基本上它由6个按钮组成,采用表格布局,并正确设置了权重属性(或者我希望是这样!)。结果看起来很好,直到我尝试设置每个按钮的背景属性。在这种情况下,按钮会采取我放置的图像的高度(宽度没问题)。例如,如果我设置大小为96px x 96px的背景图像,则结果如下所示:是否有一种方法可以防止此按钮“拉伸”,并正确居中图像?我尝试更改每个表格行的高度属性,更改按钮最大高度属性,以及更改带有图像按钮的按钮类型(并将src属性设置为所需的图标),但我没有实现所需的结果。我还阅读了谷歌关于支持多个屏幕和每个基本布局的文档,但没有找到任何解决方案。提前感谢任何愿意帮助我的人!Andrea