constraintlayout_androidApi:ConstraintLayout

Google API: https://developer.android.com/reference/android/support/constraint/ConstraintLayout

93edb41566f1d8b1c2683427b8a31414.png

常用组件和属性

  • 位置约束
  • 百分比方法设置宽高
  • 宽高比例 ratio 设置比例
  • 用 ⚪ 半径角度放置组件
  • 参照线 guideline 放置组件
  • Group 设置组内的 visibility
  • placeholder 放置组件
  • chain 链式约束
  • bias 设置左右偏移百分比

布局的理想模板(百分比)

利用bias长宽 percent可以实现百分比布局效果从而适配各种尺寸的屏幕
无论屏幕怎么变化百分比不会改变
布局的核心问题:位置和尺寸,将控件摆放的位置和控件的大小尺寸确定,理论上可以解决 UI 设计适配问题

<TextView
        android:id="@+id/textView9"
        android:text="TextView"

        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintHeight_percent="0.1"

        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintVertical_bias="0.8"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"

        />

位置约束

layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

百分比宽高

app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"

ratio 宽高比

app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintDimensionRatio="W,16:9"
app:layout_constraintDimensionRatio="16:9"

可以指定固定宽的情况下调整高,H,16:9
也可以固定高的情况下调整宽,W,16:9


圆形布局

4937fbd69c429a40bc7f29a0600c6502.png
app:layout_constraintCircle="@+id/buttonA"
app:layout_constraintCircleRadius="100dp"
app:layout_constraintCircleAngle="45"

参考线

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="100dp">
 </androidx.constraintlayout.widget.Guideline>

9fa486541c355c40cdd5d21b9c617f80.png

group 形式设置组内控件的 visibility

<androidx.constraintlayout.widget.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:constraint_referenced_ids="textView,textView2"
        >

placeholder 预留控件位置占位用

<androidx.constraintlayout.widget.Placeholder
        android:id="@+id/placeholder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="23dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5">
 </androidx.constraintlayout.widget.Placeholder>

代码中可以动态加载控件到 placeholder 中

button = findViewById(R.id.placebtn);
        placeholder = findViewById(R.id.placeholder);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                placeholder.setContentId(R.id.placebtn);
            }
        });

chain 链式约束

控件之间形成相互约束即可创建链
或者直接选中多个控件点击 create chain

b5c9357b4cc2d91e94c214d0ba8bb7d5.png

f25a83d33fef30bf361cc3372dfa27f6.png

chain 有很多种 style 设置不同的样式

在 chain head 也就是第一个控件设置 style

a9c574ecb342a0890c7fe0ed4a1282f0.png

bias 偏移百分比

需要设置水平方向位置摆放百分比需要指定
左右都要以 parant 为约束
同理需要指定为垂直方向则要指定竖直方向都为 parant 约束

<TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_bias="0.8"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值