表格布局TableLayout

简单实现计算机界面布局的案例:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">      //android:stretchColumns="*"填满多余空间的属性

<TextView
android:gravity="right|center"   //textView1的文字居右然后居中
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50dp"  //设置高度
android:text="90" />

<TableRow    //表格行
android:layout_weight="1"  //设置权重之后就能平均分配整个屏幕 ,如果不设置就有空白处
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"  //高度要设为match_parent 不然按钮就不会占满行
android:text="7" /> 

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="8" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="9" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="/" />

</TableRow>

<TableRow
android:layout_weight="1"
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="4" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="5" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="6" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="*" />

</TableRow>

<TableRow
android:layout_weight="1"
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1" />

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2" />

<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3" />

<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="-" />

</TableRow>

<TableRow
android:layout_weight="1"
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="0" />

<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="." />

<Button
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="+" />

<Button
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="=" />

</TableRow>

<TableRow
android:layout_weight="1"
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<Button
android:layout_span="4"  //最后一行只有一个按钮,设置android:layout_span就说明占4个位置(全部沾满)
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="clear" />

</TableRow>

</TableLayout>

完成之后

原文地址:https://www.cnblogs.com/chaowang/p/6095031.html