Android布局

来源:互联网 发布:mac mini 内存升级 编辑:程序博客网 时间:2024/06/11 10:49

一、布局的基本概念

 布局就是把界面中的控件按照某种规律摆放在指定的位置,主要是为了解决应用程序在不同手机中的显示问题

 Android实现布局有两种方式:

 1.代码:布局文件对应的Java类都是ViewGroup类的子类

 2.xml配置文件:所有的布局文件都是放在res/layout文件夹中


布局的基本属性

1.线性布局

 

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zking.layout.MainActivity">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_weight="2"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_weight="1"
        />
</LinearLayout>


2.表格布局

 

代码如下:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="表1"
        />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0"
        android:shrinkColumns="1"
        >
        <TableRow>
            <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="改列可收缩"
                />
        </TableRow>
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我向行方向伸展,我可以很长aaaaaaa"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我向列方向收缩,我可以很深"
                />
        </TableRow>
    </TableLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="表2"
        />


    <TableLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        android:shrinkColumns="0,2"
        >

        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第0列"
                />
            <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列"
                />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我被指定在第1列"
                android:layout_column="1"
                />
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我跨1到2列,不信你瞅瞅"
                android:layout_column="1"
                android:layout_span="2"
                />
        </TableRow>
    </TableLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="表3"
        />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1,2"
        >

        <TableRow>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="一"
                android:layout_weight="1"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="两字"
                android:layout_weight="1"
                />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="三个字"
                android:layout_weight="1"
                />
        </TableRow>


    </TableLayout>





</LinearLayout>


3.网格布局:

 

代码如下:

<?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="3"
    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="4"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        />

</GridLayout>

4.桢布局

 

代码如下:

<?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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/a3"
        android:scaleType="fitXY"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/a2"
        android:layout_gravity="center"
        />

</FrameLayout>

5.绝对布局

 

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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>

6.相对布局

da

代码如下:

<?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">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1"
            android:id="@+id/bt_relative_one"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"
            android:id="@+id/bt_relative_two"
            android:layout_below="@id/bt_relative_one"
            android:layout_toRightOf="@id/bt_relative_one"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"
            android:layout_above="@id/bt_relative_two"
            android:layout_toRightOf="@id/bt_relative_two"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"
            android:layout_toLeftOf="@id/bt_relative_two"
            android:layout_below="@id/bt_relative_two"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"
            android:layout_below="@id/bt_relative_two"
            android:layout_toRightOf="@id/bt_relative_two"
            />
    </RelativeLayout>
</RelativeLayout>

效果:


7.RTL布局

 

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本1"
        android:background="#ff0000"
        android:textSize="30sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本2"
        android:background="#00ff00"
        android:textSize="30sp"
        android:layout_marginStart="50dp"
        />
</LinearLayout>

二、布局重用

 

原创粉丝点击