【问题标题】:How to add different weight to ConstraintLayout views如何为 ConstraintLayout 视图添加不同的权重
【发布时间】:2018-07-18 13:43:07
【问题描述】:

在我的布局中,我有一个 ConstraintLayout 包含两个 TextView 元素。它们目前的尺寸相同,但我希望它们具有不同的权重,6:4 的比率。

如何在我的ConstraintLayout 中实现这一点?

【问题讨论】:

  • 你可以通过使用屏障来实现这一点

标签: android android-layout android-constraintlayout


【解决方案1】:

在 XML 中

创建水平链,然后使用app:layout_constraintHorizontal_weight属性:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#caf"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/two"
        app:layout_constraintHorizontal_weight="6"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#fac"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="4"/>

</android.support.constraint.ConstraintLayout>

在 Java 中

创建您的视图并将它们添加到父级ConstraintLayout。您需要给他们每个人一个id,以便一切正常工作;你可以使用View.generateViewId() 或者你可以为他们定义一个id 资源。

// this will be MATCH_CONSTRAINTS width and 48dp height
int height = (int) (getResources().getDisplayMetrics().density * 48);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(0, height);

View left = new View(this);
left.setId(R.id.one);
parent.addView(left, params);

View right = new View(this);
right.setId(R.id.two);
parent.addView(right, params);

然后创建一个ConstraintSet 对象并创建你的链:

ConstraintSet set = new ConstraintSet();
set.clone(parent);

int[] chainIds = { R.id.one, R.id.two }; // the ids you set on your views above
float[] weights = { 6, 4 };
set.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
                          ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
                          chainIds, weights, ConstraintSet.CHAIN_SPREAD);

set.applyTo(parent);

【讨论】:

  • 嗨,它可以通过编程方式实现吗?
  • @Demigod 我已经更新了我的答案以展示如何以编程方式进行
  • 两个元素之间的约束基本上需要定义两次的原因?我的意思是:A 在 B 的左边,和 B 在 A 的右边一样?
  • @RowanGontier 因为只有在视图之间存在双向依赖关系时才会形成链。如果 A 在 B 的左边,那么 A 依赖于 B。但如果我们不说 B 在 A 的右边,那么 B 对 A 一无所知,也无法相对于 A 调整自身的大小。
  • 谢谢,但是如果我需要一个视图宽度作为另一个视图的一半宽度,我该怎么办?
【解决方案2】:

经过一些研究,我找到了其他具有指南视图的解决方案。

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/green_color"
    android:fontFamily="@font/overpass_light"
    android:textAllCaps="true"
    android:textColor="@color/dark_grey_button"
    android:textSize="@dimen/h5"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toStartOf="parent"/>

<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/grey_text"
    android:fontFamily="@font/overpass_light"
    android:textAllCaps="true"
    android:textColor="@color/dark_grey_button"
    android:textSize="@dimen/h5"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"/>

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.6"
    />


  </android.support.constraint.ConstraintLayout>

这是上述布局的屏幕截图。您只需拖动指南视图。

【讨论】:

    【解决方案3】:

    您也可以使用app:layout_constraintWidth_percent 来实现您想要的视图。

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.6"
            android:text="6"
            android:textStyle="bold"
            android:textAlignment="center"
            android:paddingTop="15dp"
            android:background="#ffb7b7"/>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintStart_toEndOf="@id/textView1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.4"
            android:text="4"
            android:textStyle="bold"
            android:textAlignment="center"
            android:paddingTop="15dp"
            android:background="#b7b8ff"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    下面是上述布局的屏幕

    如果您需要将视图放置在垂直方向,那么您可以使用app:layout_constraintHeight_percent 并在这种情况下将android:layout_height 设置为 0dp。

    【讨论】:

      【解决方案4】:

      您可以使用 app:layout_constraintHorizontal_weight 应用链式 spread_inside 并在视图中赋予权重

      【讨论】:

        猜你喜欢
        • 2018-05-29
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 2020-08-25
        相关资源
        最近更新 更多