Android studio APP开发 第六章 界面布局讲解

Layout 界面布局

LinearLayout 线性布局

简而言之就是像一条线一样排练,可以水平排练,也可以纵向排练。 控制控件排练方式的代码:

    android:orientation="vertical" 

vertical 为垂直排列。 horizontal 为横向排列

weightSum按比重布局

重点代码:

    android:weightSum="10"

这个写总布局中。

        android:layout_weight="5"

这个写在控件中,即表示这个控件占总比重的5/10
也可以不写weightSum。 在每个控件中写layout_weight 也可以。

RelativeLayout 相对布局

相对布局即控件间的布局是相对的,某个控件是相对于某个控件的布局。先上总代码:

<?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/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button"/>
    <Button
        android:id="@+id/button_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_first"
        android:text="Hello"/>
    <Button
        android:id="@+id/button_third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_first"
        android:layout_alignParentRight="true"
        android:text="BUTTON"/>
    <Button
        android:id="@+id/button_forth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="60dp"/>
    <Button
        android:id="@+id/button_five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="26dp"
        android:text="字边距"/>

</RelativeLayout>

效果图:
相对布局效果图
代码讲解:
1.

		android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"

alignParent____ 相对于父类容器的位置,左、右、顶、底

2.

        android:layout_above="@+id/button_first"
        android:layout_below="@+id/button_first"

即相对某个控件在上面或下面。

3.

        android:layout_marginLeft="100dp"
        android:layout_marginTop="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="60dp"

layout_margin___ 相对于某一边布局,可以用距离来表示。android:layout_marginLeft=“100dp” 就是相对于左边的距离是100dp
margin还有几种布局,可以尝试。

4.

		android:paddingLeft="26dp"
        android:paddingBottom="30dp"

padding__ 是控件内部内容相对于控件边上的距离。

相对布局还有各种方式,以上几种是最常用的布局方式。

FrameLayout 帧布局

像帧一样,一帧一帧的叠加。可以在其他布局里面添加帧布局。
代码如下:

<?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/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button"/>
    <Button
        android:id="@+id/button_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_first"
        android:text="Hello"/>
    
    
    

    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="300dp">
        <Button
            android:id="@+id/button_six"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="26dp"
            android:paddingBottom="30dp"
            android:text="字边距"/>
        <Button
            android:id="@+id/button_forth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="60dp"
            />
    </FrameLayout>
    
</RelativeLayout>

AbsoluteLayout 绝对布局

绝对布局即绝对坐标,布局内部控件的坐标都是固定锁死的,不会改变。现在已经废弃。

TableLayout 表格布局

<TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TableRow>
            
        </TableRow>
    </TableLayout>

表格布局就是用来做表格的,一个Row就是一行,可以加入控件。

发布了16 篇原创文章 · 获赞 3 · 访问量 3365

猜你喜欢

转载自blog.csdn.net/Ace_bb/article/details/104033672
今日推荐