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开发入门到精通》

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

for-nothing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值