這篇博客主要探索安卓layout佈局的混合使用之TableLayout和RelativeLayout
RelativeLayout特性:允許控件佈置相對於其他控件的位置
TableLayout特性:允許控件以表格的形式排列,添加一個TableRow就添加了新的一行,
在tableRow裏面添加一個控件或者一個佈局就相當於添加了新的一列,這個控件最重要的
是一個android:layout_span屬性(它允許一個控件或者佈局佔據多列),這樣就可以隨心所欲
的創建自己喜歡的佈局了。
下面是博主自己當時測試時編寫的一個佈局文件,主要實現了讓登陸窗口懸浮在屏幕中心。
<?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">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#555">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="賬號:"/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="手機號/郵箱/qq/微信"
android:lines="1"
android:maxLength="20"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="密碼:"/>
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:hint="數字和字母"
android:lines="1"
android:maxLength="16"
android:inputType="textPassword"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/loginButton"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_span="2"/>
</TableRow>
</TableLayout>
</RelativeLayout>
注意一下,這樣做只是讓你的界面出現在屏幕中心,最重要的一點是給TableLayout配置
android:layout_centerInParent="true"這個屬性
上面的代碼做出來的界面控件離邊界有點太近了,看起來很不舒服,下面做簡單的修改:
<?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">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#555">
<TableRow
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="5dp">
<TextView
android:layout_height="wrap_content"
android:text="賬號:"/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="手機號/郵箱/qq/微信"
android:lines="1"
android:maxLength="20"/>
</TableRow>
<TableRow
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="5dp">
<TextView
android:layout_height="wrap_content"
android:text="密碼:"/>
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:hint="數字和字母"
android:lines="1"
android:maxLength="16"
android:inputType="textPassword"/>
</TableRow>
<TableRow
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/loginButton"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_span="2"/>
</TableRow>
</TableLayout>
</RelativeLayout>
爲row配置margin屬性這樣子就可以啦,當然還是看起來很水的樣子,不過你配置上背景什麼的就會好很多了