0
点赞
收藏
分享

微信扫一扫

【Android -- UI 开发】六大布局

网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。

一、布局类型

【Android -- UI 开发】六大布局_六大布局

1. LinearLayout (线性布局)

LinearLayout 是最常用的布局方式,在 XML 文件中使用 标记。它会将容器里的 UI 组件一个一个挨着排列起来。但是 LinearLayout 不会换行,当 UI 组件超出屏幕之后,则不会被显示出来。 LinearLayout 有两个重要的 XML 属性:android:gravity(对齐方式);android:orientation(排列方式)。

android:orientation(排列方式),设定了 LinearLayout 中包含的 UI 组件的排列方式,有两个选项vertical(竖向)、horizontal(横向,默认值)

android:gravity(对齐方式),设定 LinearLayout 中包含 UI 组件的对齐方式,其选项很多,常用上(top)、下(bottom)、左(left)、右(right)。

  • 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" >
</Button>
</LinearLayout>

2. RelativeLayout (相对布局)

RelativeLayout,其内子组件的位置总是相对兄弟UI组件、父亲容器来决定的。比如UI组件A相对于UI组件B的位置进行定位,那么UI组件B需要在UI组件A之前定义。

相对布局用到的主要属性:

  • android:layout_below:在某元素的下方。
  • android:layout_above:在某元素的上方。
  • android:layout_toLeftOf:在某元素的左边。
  • android:layout_toRightOf:在某元素的右边。
  • android:layout_alignXxx:控制与某元素的边界对其方式。
  • 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:text="Button1"
></Button>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="Button2"
></Button>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="Button3"
></Button>
<Button android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn2"
android:layout_toLeftOf="@id/btn3"
android:layout_above="@id/btn2"
android:text="Button4"
></Button>
</RelativeLayout>

3. TableLayout (表格布局)

表格布局,采用行、列的形式来管理UI组件,TableLayout 通过 TableRow、其他UI组件来控制表格的行数和列数。

每次向 TableLayout 中添加一个 TableRow,该 TableRow 就是一个表格行,TableRow 也是容器,因此它也可以不断添加其他组件,没添加一个子组件,该表格就增加一列。如果直接向 TableLayout 中添加组件,那么这个组件将直接占用一行。

TableLayout支持的XML属性:

  • android:collapseColumns:设置需要被隐藏的列序号。
  • android:shrinkColumns:设置需要被收缩的列序号。
  • android:stretchColumns:设置需要被拉伸的列序号。
    注意:TableLayout中所谓的序列号是从0开始计算的。

示例代码(计算器):

<?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="wrap_content"
android:padding="12dp"
android:background="@drawable/shape_layout_bg"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算器"
android:textSize="20sp"
android:textColor="@color/white"
android:layout_centerInParent="true"/>

<ImageView
android:id="@+id/iv_exit"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/ic_main_exit"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>

<EditText
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textSize="30sp"
android:padding="20dp"
android:layout_marginTop="10dp"
android:layout_gravity="end"
android:editable="false"
android:textAlignment="textEnd"
android:singleLine="true"
android:background="@drawable/shape_edt_bg"
android:text="0"/>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/ac_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:layout_margin="3dp"
android:textColor="@color/white"
android:background="@drawable/shape_btn_bg"
android:text="AC"/>

<Button
android:id="@+id/delete_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="Del"/>

<Button
android:id="@+id/percent_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="%"/>

<Button
android:id="@+id/divide_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="÷"/>

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="7"
android:id="@+id/num_seven" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="8"
android:id="@+id/num_eight" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="9"
android:id="@+id/num_nine" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="×"
android:id="@+id/multiply_btn" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="4"
android:id="@+id/num_four" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="5"
android:id="@+id/num_five" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="6"
android:id="@+id/num_six" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="-"
android:id="@+id/subtract_btn" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="1"
android:id="@+id/num_one" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="2"
android:id="@+id/num_two" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="3"
android:id="@+id/num_three" />

<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="+"
android:id="@+id/plus_btn" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/num_zero"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="0"/>

<Button
android:id="@+id/dot_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_bg"
android:text="."/>

<Button
android:id="@+id/equal_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:textSize="24sp"
android:textColor="@color/white"
android:layout_margin="3dp"
android:background="@drawable/shape_btn_equal"
android:text="="/>
</TableRow>

</TableLayout>


</LinearLayout>

4. FrameLayout (框架布局)

  • 放入的所有控件都被放置在最左上的区域;
  • 无法为控件指定一个确切的位置;
  • 下一个子控件会重叠覆盖上一个子控件。

5. AbsoluteLayout (绝对布局)

  • 采用坐标轴的方式定位控件;
  • 左上角是(0,0)点,往右X轴递增,往下Y轴递增;
  • 定位属性: android:layout_x、android:layout_y

6. ConstraintLayout (约束布局)

  • ConstraintLayout是Android Studio 2.2中主要的新增功能之一;
  • ConstraintLayout非常适合使用可视化的方式来编写界面;
  • ConstraintLayout 可以有效地解决布局嵌套过多的问题。
    目前,Android Studio 是 2.2或以上版本默认布局文件首选就是 ConstraintLayout。

基本使用请参考:​​Android新特性介绍,ConstraintLayout完全解析​​

7.选择器(Selector)

7.1 作用
通过设置选择器(selector)可使控件 在不同操作下(默认、点击等) 显示不同样式

7.2 属性

名称

说明

android:drawable

放一个drawable资源

android:state_pressed

按下状态,如一个按钮触摸或者点击。

android:state_focused

取得焦点状态,比如用户选择了一个文本框。

android:state_hovered

光标悬停状态,通常与focused state相同,它是4.0的新特性

android:state_selected

选中状态

android:state_enabled

能够接受触摸或者点击事件

android:state_checked

被checked了,如:一个RadioButton可以被check了。

7.3 实例
button_selector.xml

<?xml version="1.0" encoding="UTF-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">

< !-- 指定按钮按下时的图片 -->
<item android:state_pressed="true"
android:drawable="@drawable/start_down"
/>
< !-- 指定按钮松开时的图片 -->
<item android:state_pressed="false"
android:drawable="@drawable/start"
/>
< /selector>

在布局文件main.xml中控件的属性设置:

<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
/>


举报

相关推荐

0 条评论