前面我们分别介绍和学习了LinearLayout(线性布局)、FrameLayout(单帧布局)和AbsoluteLayout(绝对布局)。这次我们要进行RelativeLayout(相对布局)和TableLayout(表格布局)的学习。
这部分是非常重要的知识点。RelativeLayout是开发过程中强烈建议使用的,而TableLayout是满足一些特定需求时(常见表格显示,但不局限于此)须要使用。
【博客专栏:http://blog.csdn.net/column/details/alearning.html】
本例拓展的属性配置是:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 该控件位于界面中部 --> <TextView android:id="@+id/red" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerInParent="true" android:background="#CD2E57" android:gravity="center" android:text="Red" /> <!-- 该控件的左边缘与给定id为red控件的左边缘对齐 --> <TextView android:layout_width="80dp" android:layout_height="80dp" android:layout_alignLeft="@id/red" android:background="#64DB99" android:gravity="center" android:text="Green" /> <!-- 该控件位于界面底部 同一时候 位于给定id为red控件的下边/右边(即右下位置) --> <TextView android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/red" android:layout_centerInParent="true" android:layout_marginTop="20dp" android:layout_toRightOf="@id/red" android:background="#FFFA78" android:gravity="center" android:text="Yellow" /> <!-- 该控件位于给定id为red控件的左边 --> <TextView android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_toLeftOf="@id/red" android:background="#148CFF" android:gravity="center" android:text="Blue" /> </RelativeLayout>
<?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="0,1,2" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#6495ED"
android:padding="2dp"
android:text="文本1-1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#B0C4DE"
android:padding="2dp"
android:text="文本1-2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#32CD32"
android:padding="2dp"
android:text="文本1-3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#FFD700"
android:padding="2dp"
android:text="文本1-4" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_height="40dp"
android:layout_column="1"
android:layout_span="2"
android:background="#FF8C00"
android:text="跨列文本2-2,3跨2到3列" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_height="40dp"
android:layout_column="0"
android:layout_span="2"
android:background="#FF69B4"
android:text="跨列文本3-1,2跨1到2列" />
</TableRow>
<!-- -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="2"
android:shrinkColumns="1"
android:stretchColumns="0" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:background="#696969"
android:textColor="@android:color/white"
android:text="行方向伸展文本,可自行增长文本查看效果!" />
<TextView
android:textColor="@android:color/white"
android:background="#800000"
android:text="列方向伸展文本,可自行增长文本查看效果!" />
</TableRow>
</TableLayout>
</TableLayout>这部分。将会在接下来的不断的学习中了解与使用。
敬请各位看官的继续关注。
【ALearning】第四章 Android Layout组件布局(二)
原文:http://www.cnblogs.com/llguanli/p/7162354.html