线性布局LinearLayout
笔记一些容易忘记或者混淆的知识吧
LinearLayout默认排列方向是horizontal
android:gravity:文字在控件中的对齐方式
android:layout_gravity:指定控件在布局中的对齐方式
如下3个按钮分别用了这种对齐方式(默认水平排列)
android:layout_gravity="top"
android:layout_gravity="center_vertical"
android:layout_gravity="bottom"
相对布局RelativeLayout
排列方式自由性很高
例:5个button控件修改位置
android:text="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="3"
android:layout_centerInParent="true"
android:text="4"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="5"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
还有些常用的属性:
android:layout_abover (id) :表示位于该id控件的上方
android:layout_below(id): 表示位于该id控件的下方
android:layout_toLeftOf(id):左
android:layout_toRightOf(id):右
android:layout_alignLeft(id):表示让一个控件的左边缘和另一个控件的左边缘对齐
android:layout_alignRight(id):表示让一个控件的右边缘和另一个控件的右边缘对齐
android:layout_alignTop(id):表示让一个控件的上边缘和另一个控件的上边缘对齐
android:layout_alignBottom(id):表示让一个控件的下边缘和另一个控件的下边缘对齐
FrameLayout帧布局
所有的控件默认放在布局的左上角,用的场景比较少。
常用属性:
android:layout_gravity=”left” 左对齐
android:layout_gravity=”right” 右对齐
百分比局
在这个布局中,我们可以用百分比来设置控件的宽高,比例。
百分比布局只为FrameLayout和RelativeLayout进行扩展,提供了PercentFrameLayout和PercentRelativeLayout两个的全新布局。
百分比布局是定义在support库中所以我们要添加依赖库:
同步之后,xml文件如下:
重点最外层我们使用PercemtFreamLayout,百分比布局不是系统sdk中的,所以我们要添加包路径,还必须定义一个app的空间,这样才能使用百分比属性,
app:layout_widthPercent app:layout_heightPercent
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
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="com.androidbignerdranch.progressbar.MainActivity">
<Button
android:id="@+id/bt"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="1"
android:layout_gravity="left|top"/>
<Button
android:id="@+id/bt2"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="2"
/>
<Button
android:id="@+id/bt3"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="3"
android:layout_gravity="bottom|left"
/>
<Button
android:id="@+id/bt4"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="4"
android:layout_gravity="bottom|right"/>
</android.support.percent.PercentFrameLayout>