Android基础---UI布局

四大基本布局

LinearLayout–RelativeLayout–FrameLayout–TableLayout

LinearLayout (线性)横向/纵向布局

android:orientation="vertical" //横向排列 从左到右
android:orientation="horizontal" //纵向排列 从上到下

关于gravity和layout_gravity

android:layout_gravity //控件在父容器的位置
android:gravity //自己内部的元素位置
center//居中 
center_vertical //水平居中

重要属性 layout_weight :比重

<?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.example.administrator.layouttest.MainActivity">
    <EditText
        android:hint="输入字符"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <Button
        android:text="@string/app_name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>

控件所占位置等于 控件weigth/总的weigth*总的宽度

这里写图片描述

RelativeLayout 相对布局

相对布局,使用其他控件的位置来确定自己的位置。属性特别多,看一眼就会了。
相对于父容器布局—具体意思看单词就知道了,注意有些可以设置多个,比如同时设置右边和Top 就是右上角了。

android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true"

android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"

android:layout_centerInParent="true"

测试:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.administrator.layouttest.MainActivity">
    <Button
        android:text="button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
    <Button
        android:text="button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="button3"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="button4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="button5"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

这里写图片描述

相对于控件布局:

先确定其中一个控件位置,其他都相对于这个控件布局
大概有这么几组:
第一组 不会接触,有一定空隙

android:layout_above="@id/button3" //button3上方 不接触
android:layout_below="@id/button3" //button3下方 不接触
android:layout_toLeftOf="@id/button3"
android:layout_toRightOf="@id/button3"

第二种 紧接着

        android:layout_below="@id/button3"
        android:layout_alignTop="@id/button3"
        android:layout_alignLeft="@id/button3"
        android:layout_alignRight="@id/button3"

实例:略

FrameLayout

最简单的一种布局,全部在左上角,大小由最大的view决定(设置为wrap_content)。太简单了。

TableLayout

以表格的形式添加控件,其中每个TableRow为一行。每个行里可以放置不同数量的控件。如果需要合并列,可以指定该控件的layout_span数量,表示跨行。
具体使用如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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.example.administrator.layouttest.MainActivity">
    <TableRow>
        <TextView
            android:text="@string/app_name"
            android:layout_height="wrap_content"/>
        <Button android:text="@string/app_name"
            android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow>
        <Button
            android:text="@string/app_name"
            android:layout_span="2"/>
    </TableRow>
</TableLayout>

如图:
这里写图片描述

发现虽然第二行确实合并了,但是没有充分利用屏幕宽度。
指定TableLayoutstretchColumns属性,设置为1表示拉伸第二列。为0拉伸第一列。

android:stretchColumns="1"

这里写图片描述

四种基本布局就是这样,其实还有很多布局,比如网格布局等等。但都是大同小异,都是基于基本布局来的。具体可以查看继承关系。

发布了85 篇原创文章 · 获赞 40 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/lw_zhaoritian/article/details/52469118