一.LinearLayout线性布局
1.orientation的两个选择
android:orientation="vertical"垂直方向排列 ,高度此时不能match_parent;
android:orientation="horizontal"水平方向排列, 宽度此时不能match_parent
2.android:layout_gravity与android:gravity的区别
android:gravity是指定文字在控件中的对齐方式;
android:layout_gravity是指定控件在布局中的对齐方式,值得注意的是对齐方式还与orientation属性有关,horizontal时只有垂直的对齐方式有效,同理vertical只有水平有效
3.用android:layout_weight属性来按比例分割屏幕
二.RelativeLayout相对布局
第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
三.FrameLayout帧布局
这种布局相比前两种,应用比较少,没有方便定位方式,所有控件都会默认摆放在左上角
四。百分比布局
第一种布局第四点我提到了按比例分割屏幕的android:layout_weight属性,可对于另外两种却没有这种切割比例。所以Android引入一种新的百分比布局来解决他们的尴尬。在这种布局中我们可以不再使用wrap_content,match_parent等方式来指定控件大小,而是可以直接指定控件在布局中占百分比,在这种方式下,我们就能轻松实现平分布局甚至任意比例分割布局的效果。
PercentFrameLayout和PercentRelativeLayout,
如何用呢首先在在gradle的depenencies闭包中中添加
compile 'com.android.support:percent:23.2.1'
记住那个红色划线数字得相同跟着你上面那个数
接着直接在xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a dialog activity"/>
<Button
android:id="@+id/button_11"
android:text="Button 1"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button_12"
android:text="Button 2"
android:layout_gravity="left|bottom"
app:layout_widthPercent="80%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/button_13"
android:text="Button 3"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="30%"/>
<Button
android:id="@+id/button_14"
android:text="Button 4"
android:layout_gravity="right|bottom"
app:layout_widthPercent="20%"
app:layout_heightPercent="70%"/>
</android.support.percent.PercentFrameLayout >
xmlns:app="http://schemas.android.com/apk/res-auto"
这一句要怎么来 只要你在下面控件输入app然后alt+enter就有提示修复gradle还要再来一次才有这一句