Android七种布局

来源:互联网 发布:淘宝宝贝宣言 编辑:程序博客网 时间:2024/05/29 19:27

一些布局的通用基本属性:



1.线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

相关信息:


实例:

效果图:


实现代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="左上按钮"            />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            >            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="右上按钮"                android:layout_gravity="right"                />        </LinearLayout>    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:layout_gravity="center"        >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="中间按钮"            android:layout_gravity="center"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按钮左下"            android:layout_gravity="bottom"            />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:orientation="vertical"            >            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="右下按钮"                android:layout_gravity="right"                />        </LinearLayout>    </LinearLayout></LinearLayout>

2. 帧布局

帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。

实例:

效果图


实现代码:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent">    <TextView        android:layout_width="300dp"        android:layout_height="300dp"        android:background="#ff0000"        android:layout_gravity="center"        />    <TextView        android:layout_width="250dp"        android:layout_height="250dp"        android:background="#00ff00"        android:layout_gravity="center"        />    <TextView        android:layout_width="200dp"        android:layout_height="200dp"        android:background="#000000"        android:layout_gravity="center"        />    <TextView        android:layout_width="150dp"        android:layout_height="150dp"        android:background="#f5e42e"        android:layout_gravity="center"        /></FrameLayout>
3.表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。

相关信息:

实例:

效果图:


实现代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TableLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    >    <TableRow android:background="#ff0000">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="这是第一个表的第一行"            android:layout_column="0"            android:layout_span="2"            android:layout_weight="1"            />    </TableRow>    <TableRow>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="2行1列"            android:background="#ff0000"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="2行2列"            android:layout_weight="1"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="2行3列"            android:layout_weight="1"            android:background="#00ff00"            />    </TableRow></TableLayout>    <TableLayout        android:layout_width="600dp"        android:layout_height="wrap_content"        android:collapseColumns="1"        >        <TableRow>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="这是第二个表的第1行第1列"                android:background="#00ff00"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="这是第二个表的第1行第2列"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="这是第二个表的第1行第3列"                />        </TableRow>        <TableRow>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="第2行1列"                android:background="#00ff00"                />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="第2行2列"                />            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="第2行3列"                />        </TableRow>    </TableLayout></LinearLayout>


4.相对布局相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
相关信息:
实例:
效果图:
实现代码:
<?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">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮1"        android:id="@+id/button_relative_b1"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮2"        android:id="@+id/button_relative_b2"        android:layout_toRightOf="@id/button_relative_b1"        android:layout_below="@id/button_relative_b1"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮3"        android:layout_toRightOf="@id/button_relative_b2"        android:layout_above="@id/button_relative_b2"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮4"        android:layout_toLeftOf="@id/button_relative_b2"        android:layout_below="@id/button_relative_b2"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮5"        android:layout_toRightOf="@id/button_relative_b2"        android:layout_below="@id/button_relative_b2"        /></RelativeLayout>
5. 绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。不推荐使用。
实例:
效果图:
实现代码:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绝对布局"        android:layout_x="150dp"        android:layout_y="150dp"        />    <include layout="@layout/public_title"></include></AbsoluteLayout>

下面两个是Android 4.0后才有的两种布局:
6.网格布局
和表格布局类似但要优于表格布局
相关信息:
实例:
效果图:
实现代码:
<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent"    android:rowCount="6"    android:columnCount="4"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="2"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="3"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="/"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="4"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="5"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="6"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="x"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="7"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="8"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="9"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="-"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="0"        android:layout_columnSpan="2"        android:layout_gravity="fill_horizontal"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="."        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="+"        android:layout_rowSpan="2"        android:layout_gravity="fill_vertical"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="="        android:layout_columnSpan="3"        android:layout_gravity="fill_horizontal"        /></GridLayout>


原创粉丝点击