Android app开发学习笔记——四大布局
一、LinearLayout(线性布局)
1.设置排列方式
android:orientation="horizontal"<!--水平排列-->
android:orientation="vertical"<!--垂直排列-->
2.android:layout_gravity(对齐方式)
layout_gravity 是LinearLayout子元素的特有属性。
对于layout_gravity,该属性用于设置控件相对于容器的对齐方式,可选项有
top、bottom、left、right、center_vertical、center_horizontal、center、fill
等
ayout_gravity与gravity的区别参考:
Android 中(match_parent,wrap_content,与fill_parent)三者区别,(layout_gravity与gravity)的区别
3. weight(权重)
LinearLayout布局中layout_weight属性用来分配View在LinearLayout中占用空间,只有LinearLayout包裹的View才有这个属性
它是用来分配剩余空间的一个属性
如果想用layout_weight平均分配空间,正确方式是将layout_width(或layout_height)设置为0dp,再通过layout_weight按比例分配空间即可,而不是layout_width设为match_parent或者wrap_content。
原因参考
Android中LinearLayout布局中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 | 位于父元素的上边缘 |
属性值为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 | 该元素的右边缘和某元素的的右边缘对齐 |
属性为具体的值
相对像素值如30dp、40dp
属性 | 含义 |
---|---|
android:layout_marginBottom | 底边缘的距离 |
android:layout_marginLeft | 左边缘的距离 |
android:layout_marginRight | 右边缘的距离 |
android:layout_marginTop | 上边缘的距离 |
三、框架布局
框架布局是最简单的布局方式,所有添加到这个布局中的视图都是以层叠的方式显示,第一个添加到框架布局中的视图显示在最底层,最后一个会放在最顶层,上层视图会覆盖下层视图。
向其中添加控件时,所有组件都会置于这块区域的左上角,同一时刻只能看到最上面的那个组件。
当然可以为组件添加layout_gravity属性,从而指定对齐方式。
四、约束布局
1.为什么要用ConstraintLayout(约束布局)
约束布局可以说是现在最常见的布局
因为在开发过程中经常能遇到一些复杂的UI,可能会出现布局嵌套过多的问题,嵌套得越多,设备绘制视图所需的时间和计算功耗也就越多
有些人考虑到了嵌套布局带来的风险,所以用一个RelativeLayout来装下所有的控件。那么问题来了,既然用RelativeLayout可以解决问题,为什么还要使用ConstraintLayout呢?因为ConstraintLayout使用起来比RelativeLayout更灵活,性能更出色!还有一点就是ConstraintLayout可以按照比例约束控件位置和尺寸,能够更好地适配屏幕大小不同的机型
2.添加权重控制比例
属性 | 含义 |
---|---|
layout_constraintHorizontal_weight | 横向的权重 |
layout_constraintVertical_weight | 纵向的权重 |
2.当相对布局用
属性 | 含义 |
---|---|
layout_constraintLeft_toLeftOf | 当前View左边在某个View的左边,可以是parent与某个View的ID |
layout_constraintLeft_toRightOf | 当前View左边在某个View的右边,可以是parent与某个View的ID |
layout_constraintRight_toRightOf | 当前Viewr的右边在某个View的右边,可以是parent与某个View的ID |
layout_constraintRight_toLeftOf | 当前Viewr的右边在某个View的左边,可以是parent与某个View的ID |
layout_constraintBottom_toBottomOf | 当前Viewr的下边在某个View的下边,可以是parent与某个View的ID |
layout_constraintBottom_toTopOf | 当前Viewr的下边在某个View的上边,可以是parent与某个View的ID |
layout_constraintTop_toTopOf | 当前Viewr的上边在某个View的上边,可以是parent与某个View的ID |
layout_constraintTop_toBottomOf | 当前Viewr的上边在某个View的下边,可以是parent与某个View的ID |
例如
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
3.当百分比布局用
属性 | 含义 |
---|---|
layout_constraintVertical_bias | 垂直偏移率。 |
layout_constraintHorizontal_bias | 水平偏移率。 |
layout_constraintHeight_percent | 高度百分比,占父类高度的百分比 |
layout_constraintWidth_percent | 宽度百分比,占父类宽度的百分比 |
例如
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7"
角度定位,尺寸约束等可以参考
约束布局ConstraintLayout看这一篇就够了
五、动态添加布局
有时布局文件的View数量是不固定的,需要根据逻辑来判断要添加多少View。这种情况下,只能在代码中来添加View.
给
初始化
private ConstraintLayout buju;
private int i=0;
监听器中增加布局
buju = (ConstraintLayout)findViewById(R.id.yueshu);
findViewById(R.id.Button_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView addtext= new TextView(this);
addtext.setText("你添加了我");
addtext.setPadding(0,i,0,buju.getHeight()-100-i);
i=i+100;
buju.addView(addtext);//通过addView方法动态添加控件
}
});
部分代码及内容参考自《Android app开发入门到精通》