今天来讲一下布局的知识,首先常用的就是线性布局,因为拿到规划图第一个要写的就是布局,能不能设计出心里想要的结果,全看自己对于布局的掌握如何,所以说布局还是有一定的重要性。
注意的是,这个文章只讲了大概的使用办法,具体还需多加练习,有问题可以留言或者私信。
布局管理器LinearLayout——线性布局
1.最常用的属性
android:id 布局的标识
android:layout_width:布局的宽度
android:layout_height:布局的高度
android:background:布局的背景,可选颜色,图片等
android:layout_margin:布局的外边距
android:layout_padding:布局的内边距
android:orientation:布局内的排列方式
2.一般的线性布局的常见的定义
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".CustomDialogActivity"
android:padding="15dp"
android:background="@color/colorDark">
<Button
android:id="@+id/bt_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
</LinearLayout>
效果如图所示:
3.布局嵌套布局
也可以在布局中套布局如下代码
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".CustomDialogActivity"
android:padding="15dp"
android:background="@color/colorDark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/colorAccent">
<Button
android:id="@+id/bt_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
</LinearLayout>
</LinearLayout>
在第一个线性布局中又写了一个线性布局,效果如下图所示
4.一些属性介绍
(1)关于android:orientation属性
vertical 垂直排列
horizontal水平排列
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".CustomDialogActivity"
android:padding="15dp"
android:background="@color/colorDark"
android:orientation="vertical"> //两个线性布局是垂直排列
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
android:orientation="horizontal" //里面的组件是水平排列
android:layout_marginBottom="30dp">
<Button
android:id="@+id/bt_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/colorAccent"
android:orientation="vertical">//里面的组件是水平排列
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
</LinearLayout>
</LinearLayout>
效果如图所示,具体可以自己看代码理解,学习效果最明显
(2)android:gravity的属性设置,是标志布局内的组件放在什么位置,如下代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/colorAccent"
android:orientation="vertical"
android:gravity="center">//放在中间位置进行排列
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义dialog"
/>
</LinearLayout>
效果如图,其他放置位置可以自己选择
(3)android:weight权重的意思,可以去根据权重来分割整个布局的长度或者宽度
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
android:orientation="horizontal"
android:layout_marginBottom="30dp">
<view
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1">
</view>
<view
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:layout_weight="1">
</view>
</LinearLayout>
效果如下图所示,两个view的weight都是1,则两个各占布局的1/2,等用的时候可以实践一下