如何在ConstraintLayout中设置布局权重

时间:2018-05-29 06:50:08

标签: android android-constraintlayout

我有4次观看&我需要将它们分成2和2组。然后让它们覆盖在该地区。

我尝试使用Group& chainStyle但是没有什么能满足100%。

我想, enter image description here

我成功了, enter image description here

下面给出了相同的代码,

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

5 个答案:

答案 0 :(得分:3)

你也可以使用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> 

看看这是否符合你的要求。

答案 1 :(得分:1)

以下是解决方案:

<?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_marginTop="8dp"
    android:layout_height="wrap_content">
<RelativeLayout
    android:id="@+id/button_savewd"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="#3b5998"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/asdfsff"
    tools:ignore="RtlCompat">
    <ImageView
        android:id="@+id/button_save"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Facebook"
        tools:ignore="ContentDescription" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/button_save"
        android:layout_toRightOf="@+id/button_save"
        android:background="@color/white"
        android:text="facebook"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button_save"
        app:layout_constraintRight_toRightOf="parent" />
</RelativeLayout>
<RelativeLayout
    android:id="@+id/asdfsff"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginStart="4dp"
    android:background="#444"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/button_savewd"
    app:layout_constraintRight_toRightOf="parent"
    tools:ignore="RtlCompat">
    <ImageView
        android:id="@+id/img_twitter"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:src="@drawable/logo_0"
        android:text="Facebook"
        tools:ignore="ContentDescription" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/img_twitter"
        android:layout_toRightOf="@+id/img_twitter"
        android:text="Twitter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/img_twitter"
        app:layout_constraintRight_toRightOf="parent" />
     </RelativeLayout>
 </android.support.constraint.ConstraintLayout>
  

将每个项目的宽度设置为0dp或MATCH_CONSTRAINT让视图填充父项(可选)

输出:

enter image description here

答案 2 :(得分:1)

您应该修改以下代码

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

<ImageView
   android:id="@+id/ivFb"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="32dp"
   android:clickable="false"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toStartOf="@+id/tvFacebook"
   app:layout_constraintHorizontal_bias="0.0"
   app:layout_constraintHorizontal_chainStyle="spread"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:srcCompat="@drawable/ic_android_black_24dp"
   tools:ignore="ContentDescription" />

<TextView
   android:id="@+id/tvFacebook"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_marginEnd="16dp"
   android:clickable="false"
   android:gravity="center"
   android:text="Facebook"
   app:layout_constraintBottom_toBottomOf="@+id/ivFb"
   app:layout_constraintEnd_toStartOf="@+id/ivTwitter"
   app:layout_constraintHorizontal_bias="0.5"
   app:layout_constraintHorizontal_chainStyle="spread"
   app:layout_constraintStart_toEndOf="@+id/ivFb"
   app:layout_constraintTop_toTopOf="parent" />


<ImageView
   android:id="@+id/ivTwitter"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="16dp"
   android:clickable="false"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toStartOf="@+id/tvTwitter"
   app:layout_constraintHorizontal_bias="0.5"
   app:layout_constraintHorizontal_chainStyle="spread"
   app:layout_constraintStart_toEndOf="@+id/tvFacebook"
   app:layout_constraintTop_toTopOf="parent"
   app:srcCompat="@drawable/ic_android_black_24dp"
   tools:ignore="ContentDescription" />

 <TextView
   android:id="@+id/tvTwitter"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_marginEnd="32dp"
   android:clickable="false"
   android:gravity="center"
   android:text="Twitter"
   app:layout_constraintBottom_toBottomOf="@+id/ivTwitter"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.5"
   app:layout_constraintHorizontal_chainStyle="spread"
   app:layout_constraintStart_toEndOf="@+id/ivTwitter"
   app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

看起来像this

我修改了你的代码,用于在约束布局中排列元素,现在你可以根据需要修改代码了:)

答案 3 :(得分:0)

应用以下步骤:

  1. 选择您的小部件并将链设置为水平
  2. 选择您的小部件并将其设置为“水平居中”
  3. 选择您的小部件并将其设置为“组织”(水平扩展)。

答案 4 :(得分:0)

使用chainStyle很简单,必须给定开始和结束约束(如果chainStyle = horizo​​ntal),而chainStyle = vertical的顶部和底部约束