1.为了防止TableLayout里面的控件超出显示屏幕的宽度
加上下面这句话,使得它可以伸缩
android:shrinkColumns="1"
2.TableLayout(不用TableRow)每一个控件成为一行,用TableRow,一个TableRow里面所有的控件自成一行
3.在使用TableRow的时候需要注意,每一个TableRow的每一个控件都和所在的列里面最长width最大的那个对齐,并且每一行的每一个控件都相互对齐
比如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.radiogrouppractice.MainActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="1">
<!--第一行“姓名+编辑框”-->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textColor="#000000"
android:textSize="20dp"/>
<EditText
android:id="@+id/edt"
android:layout_height="wrap_content"
android:layout_width="200dp"
/>
</TableRow>
<!--第二行 “血型”+RadioGroup-->
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="血型:"
android:textSize="20dp"
android:textColor="#000000"/>
<RadioGroup
android:id="@+id/RG"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O型"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A型"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B型"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AB型"
android:textSize="20dp"/>
</RadioGroup>
</TableRow>
<TextView
android:id="@+id/tv"
android:textSize="20dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</TableLayout>
</RelativeLayout>
如果下面加3个按钮就变成:
如果下面只有一个按钮,就算你设置成fill_parent也不会铺满整个TableRow。