线性布局LinearLayout
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#fff"
>
</LinearLayout>
android:orientation属性可以设置为水平horizontal和垂直vertical
再线性布局中的控件可以使用layout_gravity属性设置控件对布局的对齐方式
线性布局中控件的layout_weight属性设置宽度或高度百分比布置。
相对布局RelativeLayout
它默认所有子控件都在左上角.
布局中控件可设置layout_centerInParent=”true”等类似属性进行相对于父布局的布置。
也可以使用layout_above=”@id/button03” 等类似的方式进行相对与其他控件的布局,要注意的是,作为参照物的控件要放在设置属性控件的前面。
简单测试:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="按钮4"
android:layout_alignParentEnd="true" />
<Button
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5"/>
</RelativeLayout>
![](https://i-blog.csdnimg.cn/blog_migrate/878b61262b261166da4c38640d7d54a5.png)
<?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/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="按钮1"
/>
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/button01"
android:layout_above="@id/button01"
android:text="按钮2"
android:layout_toStartOf="@+id/button01" />
<Button
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button01"
android:layout_above="@id/button01"
android:text="按钮3"
android:layout_toEndOf="@id/button01" />
<Button
android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button01"
android:layout_below="@id/button01"
android:text="按钮4"
android:layout_toStartOf="@id/button01" />
<Button
android:id="@+id/button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button01"
android:layout_below="@id/button01"
android:text="按钮5"
android:layout_toEndOf="@id/button01" />
</RelativeLayout>
![](https://i-blog.csdnimg.cn/blog_migrate/70caca8c5d13e2e4aa814c131ab7368a.png)
帧布局FrameLayout
子控件可以使用layout_gravity=”right”来进行布置,用的比较少。
百分比布局
需要添加库依赖,不知道如何添加可查看文章: http://beyondcbz.xyz/?p=228
使用第一种方法搜索:com.android.support:percent:28.0.0
版本可能不一样,找到后添加就好了。
可以在和main文件夹同级的文件build.gradle
![](https://i-blog.csdnimg.cn/blog_migrate/b21d37fa7b4f95455047a5209c26e48a.png)
打开可以看到:
![](https://i-blog.csdnimg.cn/blog_migrate/29bf51151e711d20046d3a1e3f6aa876.png)
这就是添加的依赖。之后其他部分也会有添加依赖的操作。
百分比布局还有点问题,之后再看看。