如何为 ConstraintLayout 视图添加不同的权重

新手上路,请多包涵

在我的布局中,我有一个 ConstraintLayout 包含两个 TextView 元素。它们目前大小相同,但我希望它们具有不同的重量,比例为 6:4

这如何在我的 ConstraintLayout 中实现?

原文由 Prashant Jajal 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 846
2 个回答

在 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>

在此处输入图像描述

在爪哇

创建您的视图并将它们添加到父 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);

原文由 Ben P. 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

 <?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>

这是上面布局的屏幕截图。您只需拖动指南视图即可。 拖动 % 希望显示您的指南视图

原文由 Prashant Jajal 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏