Android程序打包及五种常用布局

Android程序打包:<https://blog.csdn.net/woaichimahua/article/details/54427528
在这里插入图片描述

1.线性布局

主要以水平或垂直方式来显示界面中的控件。当控件水平排列时,显示顺序依次为从左到右,当控件垂直排列时,显示顺序依次为从上到下。

layout_width

layout_width

决定布局组件的宽度和高度,取值有一下三种:

wrap_content 刚刚把文字组件包裹满的长度

match_parent 撑满整个父空间的长度

100px 具体的像素值

最好给组件设置id,养成好的习惯,例如:android:id="@+id/button_1"s

设置组件竖直还是水平并排的属性 orientation

取值有两种:vertical(垂直)和 horizontal(水平)

为了解决一行组件并排不能完全显示的问题而使用权重 weight

如以下,按钮1占一行的1/4,按钮3则占一行的2/4

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮1" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮2" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="按钮3" />
</LinearLayout>

在这里插入图片描述

2.相对布局

通过相对定位的方式指定控件位置,即以其它控件或父容器为参照物,摆放控件位置。

在设计相对布局时要遵循控件之间的依赖关系,后放入控件的位置依赖于先放入的控件。

layout_alignParentBottom属性紧贴父元素下边缘,取值只能为true或false。

layout_centerHorizontal属性水平居中。

layout_marginTop属性所在控件距上部最近控件的最小值。

layout_marginBottom属性所在控件距下部最近控件的最小值。

layout_alignBottom属性的下边缘和指定ID组件的下边缘对齐。

layout_toRightOf属性所在的控件在指定ID组件右边。

<?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:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="按钮1" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="260dp"
        android:text="按钮2" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/btn_two"
        android:layout_marginBottom="100dp"
        android:layout_toRightOf="@id/btn_two"
        android:text="按钮3" />

</RelativeLayout>

在这里插入图片描述

3.帧布局

为每个加入其中的控件创建一个空白区域(称为一帧,每个控件占据一帧)。

所有控件都默认显示在屏幕左上角,按照先后放入的顺序重叠摆放。帧布局的大小由内部最大控件的决定。

foreground设置前景图片。

foregroundGravity

<?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"
    android:foreground="@mipmap/ic_launcher"
    android:foregroundGravity="left">

    <Button
        android:layout_width="300dp"
        android:layout_height="450dp"
        android:text="按钮1" />

    <Button
        android:layout_width="200dp"
        android:layout_height="210dp"
        android:text="按钮2" />

</FrameLayout>

在这里插入图片描述

4.表格布局

表格布局继承LinearLayout线性布局

以表格形式排列控件的,通过行和列将界面划分为多个单元格,每个单元格都可以添加控件。

表格布局不需要制定多少行列,布局内每添加一行TableRow表示添加一行,然后再在TableRow添加子控件,容器的列数由包含列数最多的行决定。

stretchColumns为TableLayout容器里面设置属性,表示 被设置的这些列可拉伸 (注意:TableLayout中列的索引从0开始)。

shrinkColumns为TableLayout容器里面设置属性,表示 被设置的这些列可收缩。

layout_column在容器里面的控件设置属性,指定该控件在TableRow中的指定列。

<?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"
    android:stretchColumns="2">

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:text="按钮1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮2" />
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮4" />
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮5" />
    </TableRow>
</TableLayout>

在这里插入图片描述

5.绝对布局

通过指定x、y坐标来控制每一个控件位置。

实际应用中一般不适用AbsoulteLayout,因为应用该APP的手机屏幕大小,分辨率会不同.

layout_x指定控件在容器中的 x 坐标值。

layout_y指定控件在容器中的 y 坐标值。

<?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">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="50dp"
        android:layout_y="50dp"
        android:text="按钮1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="200dp"
        android:layout_y="150dp"
        android:text="按钮2" />
</AbsoluteLayout>

在这里插入图片描述

发布了16 篇原创文章 · 获赞 5 · 访问量 731

猜你喜欢

转载自blog.csdn.net/qq_18625571/article/details/104301350
今日推荐