我有 4 个 View ,我需要将它们分成 2 个一组,然后让它们覆盖该区域。

我尝试使用 GroupchainStyle 但没有任何东西可以 100% 满足。

我想,
android - 如何在 ConstraintLayout 中设置布局权重-LMLPHP

我设法做到了这一点,
android - 如何在 ConstraintLayout 中设置布局权重-LMLPHP

下面给出了相同的代码,

<?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="wrap_content"
android:background="@color/colorBtnPink"
android:paddingTop="@dimen/_10sdp"
android:padding="@dimen/_10sdp">

<ImageView
    android:id="@+id/ivFb"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnFbDark"
    android:clickable="false"
    android:padding="@dimen/_10sdp"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_facebook"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/tvFacebook"
    style="@style/tvSmallPopMedium"
    android:layout_width="0dp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnFacebook"
    android:clickable="false"
    android:gravity="center"
    android:paddingBottom="@dimen/padding_regular"
    android:paddingTop="@dimen/padding_regular"
    android:text="@string/action_facebook"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/ivFb"
    app:layout_constraintTop_toTopOf="parent" />


<ImageView
    android:id="@+id/ivTwitter"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnTwDark"
    android:clickable="false"
    android:padding="@dimen/_10sdp"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/tvFacebook"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_twitter"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/tvTwitter"
    style="@style/tvSmallPopMedium"
    android:layout_width="0dp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnTwitter"
    android:clickable="false"
    android:gravity="center"
    android:paddingBottom="@dimen/padding_regular"
    android:paddingTop="@dimen/padding_regular"
    android:text="@string/action_twitter"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/ivTwitter"
    app:layout_constraintTop_toTopOf="parent" />
 </android.support.constraint.ConstraintLayout>

最佳答案

您还可以使用 guideLines 属性,如下所示:

<?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="wrap_content"
android:background="@color/colorBtnPink"
android:padding="@dimen/_10sdp"
android:paddingTop="@dimen/_10sdp">

<ImageView
    android:id="@+id/ivFb"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnFbDark"
    android:clickable="false"
    android:padding="@dimen/_10sdp"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_facebook"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/tvFacebook"
    style="@style/tvSmallPopMedium"
    android:layout_width="0dp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnFacebook"
    android:clickable="false"
    android:gravity="center"
    android:paddingBottom="@dimen/padding_regular"
    android:paddingTop="@dimen/padding_regular"
    android:text="@string/action_facebook"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/ivFb"
    app:layout_constraintRight_toLeftOf="@id/ivTwitter"
    app:layout_constraintTop_toTopOf="parent" />


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


<ImageView
    android:id="@+id/ivTwitter"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:background="@color/colorBgBtnTwDark"
    android:clickable="false"
    android:padding="@dimen/_10sdp"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/tvFacebook"
    app:layout_constraintStart_toStartOf="@+id/guideline2"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_twitter"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/tvTwitter"
    style="@style/tvSmallPopMedium"
    android:layout_width="0dp"
    android:layout_height="@dimen/_40sdp"
    android:background="@color/colorBgBtnTwitter"
    android:clickable="false"
    android:gravity="center"
    android:paddingBottom="@dimen/padding_regular"
    android:paddingTop="@dimen/padding_regular"
    android:text="@string/action_twitter"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toRightOf="@+id/ivTwitter"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

看看这是否满足您的要求。

关于android - 如何在 ConstraintLayout 中设置布局权重,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50577988/

10-09 12:46