ConstraintLayout的constraintDimensionRatio属性记录

本文详细解析了Android ConstraintLayout中布局元素宽高比的设定规则与逻辑,包括不同情况下的默认约束方向及如何通过属性调整实现精确的尺寸控制。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    android:orientation="vertical"
    tools:ignore="MissingConstraints">

    <!--
    w:0dp
    h:wrap_content/具体数值
    此时默认是以h作为标准来约束w,"w,2:1"="2:1"都是宽:高=2:1,若此时你写"h,2:1"则代表高:宽=2:1;-->
    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="3:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!--
    w:wrap_content/具体数值
    h:0dp
    此时默认是以w作为标准来约束h,"h,2:1"="2:1"都是宽:高=2:1,若此时你写"w,2:1"则代表高:宽=2:1;-->
    <TextView
        android:id="@+id/tv2"
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv1"/>
    <!--
       w:0dp
       h:0dp
       此时默认是以w作为标准来约束h,"h,2:1"="2:1"都是宽:高=2:1,若此时你写"w,2:1"则代表高:宽=2:1。
       私以为ratio比例值并非一定是h,
       ratio代表h = w * ratio,w,ratio代表w = h * ratio,
       而是你写的是否和官方默认的规则相同,若相反才是反比。
       -->
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:background="@color/colorAccent"
        app:layout_constraintDimensionRatio="3:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv2"/>


</android.support.constraint.ConstraintLayout>

 

### ConstraintLayout 中 `layout_constraintDimensionRatio` 的使用方法 `layout_constraintDimensionRatio` 是 `ConstraintLayout` 中用于控制视图宽高比例的重要属性。通过该属性,可以确保视图在不同屏幕尺寸或布局约束下保持特定的宽高比。例如,一个图片视图可能需要始终以 16:9 的比例显示,无论其实际宽度或高度如何变化。 在代码中,可以通过 `ConstraintSet` 来动态设置 `layout_constraintDimensionRatio`。例如,以下代码片段展示了如何通过 `ConstraintSet` 来设置一个视图的宽高比为 9:16: ```java ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(mRlContent); constraintSet.setDimensionRatio(R.id.iv, "h,9:16"); constraintSet.applyTo(mRlContent); ``` 此代码首先创建了一个 `ConstraintSet` 实例,并克隆了当前布局的状态。然后,通过调用 `setDimensionRatio` 方法,将指定视图的宽高比设置为 9:16,并应用更改到布局上 [^1]。 ### 设置 `layout_constraintDimensionRatio` 的 XML 示例 在 XML 文件中,可以直接使用 `app:layout_constraintDimensionRatio` 属性来设置宽高比。例如,以下 XML 代码展示了如何将一个视图的宽高比设置为 2:1: ```xml <View android:id="@+id/view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="w,2:1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> ``` 在这个例子中,视图的宽度和高度都被设置为 `0dp`,这意味着它们将根据约束条件来确定大小。`layout_constraintDimensionRatio` 属性的值 `"w,2:1"` 表示宽度是基准,宽高比为 2:1 [^2]。 ### 注意事项 - **宽高设置为 `0dp`**:为了使 `layout_constraintDimensionRatio` 生效,通常需要将视图的宽度和高度设置为 `0dp`。这样,视图的实际大小将由约束条件和宽高比共同决定。 - **选择基准维度**:当宽高比被指定时,需要确定哪个维度是固定的。例如,如果宽度是固定的,则高度将根据宽高比进行调整,反之亦然。 - **动态调整**:在某些情况下,可能需要在运行时动态调整宽高比。此时,使用 `ConstraintSet` 是一种有效的方法 [^2]。 通过合理使用 `layout_constraintDimensionRatio`,可以在 `ConstraintLayout` 中实现更加灵活和响应式的界面设计。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值