安卓开发的五大布局,FrameLayout(帧布局)、LinearLayout(线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)以及绝对布局,绝对布局一般不使用,容易导致布局文件在不同手机下显示的错乱。线性布局下主要属性有:
android:layout_height //设置组件基本高度
android:layout_width //设置组件宽度
android:orientation //设置线性下的水平布局(horizontal)或者垂直布局(vertical)
android:id //设置组件的id属性
android:background //背景图片
android:gravity //设置相对于组件的对齐方式,例如top、bottom、left、right、center等,不同的对齐方式可以同时使用
android:layout_gravity //与gravity相似,但是android:layout_gravity是设置相对于父容器的位置关系
表格布局的基本框架:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
各个组件
<TableRow/>
<TableLayout/>
表格布局结合线性布局的基本框架:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
<LinearLayout>
各个组件
<LinearLayout/>
<TableRow/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....>
<LinearLayout>
各个组件
<LinearLayout/>
<TableRow/>
<TableLayout/>
下面是登陆界面
下面是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tablelayout1"
android:stretchColumns="0,4"
android:background="@drawable/landing" //设置背景图片landing
>
<TableRow android:id="@+id/tablerow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom" >
<EditText //账号编辑框
android:id="@+id/leditText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="30" //最大输入字符数,可按实际要求修改
android:hint="请输入帐号" //刚开始时编辑框默认显示的内容
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/leditText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="16"
android:inputType="textPassword"
android:hint="请输入密码" >
<requestFocus android:layout_width="wrap_content" />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textSize="18sp" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tablerow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="bottom" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="left|top"
android:text="忘记密码"
android:textSize="13sp" />
<Button //该按钮的作用主要是为了使得“忘记密码”以及“注册”两个按钮在两旁显示,设置其layout_weight为1,则其他两个按钮会在两旁
android:alpha="0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:layout_gravity="right|top"
android:gravity="right|top"
android:text="注册 "
android:textSize="13sp" />
</LinearLayout>
</TableRow>
</TableLayout>