Android开发---UI界面

87 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第10天,点击查看活动详情

一、布局

常用布局:线性布局、相对布局、帧布局、表格布局、绝对布局

1.线性布局LinearLayout

android:orientation :指定控件排布方向

android:layout_width="match_parent"

android:layout_height="match_parent" 设置布局容器的宽度高度

2.相对布局

知识点:

1)布局初始状态都相对于左上角

2)控件需要指明id,用于设置相对位置时定位

3)layout_below:控件下方,类似定位配置可参见文档

3.帧布局FrameLayout

//线性布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">


//相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

//文本框:

<TextView

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请输入号码"/>

//输入文本框:

<EditText

android:id="@+id/et_number"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/tv"/>

//按钮:

<Button

android:id="@+id/btn_call"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/et_number"

android:text="拨打"/>

</RelativeLayout>

3.帧布局

知识点:帧布局为一层一层显示

layout_gravity="center" 父容器中间

android:textColor="#FF0000" 文字颜色

android:background="#000000" 背景颜色

4.表格布局TableLayout

知识点:一个TableRow标签代表一行,里面有几个控件就是几列,此为TableLayout不是GridLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:id="@+id/tv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="请输入号码"

android:textColor="#FF0000"

android:background="#000000"/>

<Button

android:id="@+id/et_number"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"/>

</FrameLayout>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TableRow>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF0000"

android:text="11"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF00FF"

android:text="12"/>

</TableRow>

5.绝对布局(已经废弃)

知识点:绝对布局已经废弃。layout_x和layout_y用于定位控件

6.约束布局

1)定位控件

2)辅助定位

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF0000"

android:layout_x="100dp"

android:layout_y="200dp"

android:text="11"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#FF00FF"

android:text="12"/>

</AbsoluteLayout>barrier https://www.jianshu.com/p/1ca5c53b96b2

3)工具栏

4)创建横屏布局

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第10天,点击查看活动详情