ConstraintLayout:如何将视图设置为屏幕宽度的一半并居中?

Sof*_*ion 17 android android-constraintlayout

TD; DR
视图宽度必须恰好是屏幕的一半,并且居中.使用ConstraintLayout.

请注意,视图没有任何内部宽度.

<View android:background="#ff0000" ... />
Run Code Online (Sandbox Code Playgroud)

原始问题
我想实现一个布局,其中视图大小是屏幕大小的一半,并且水平居中.

这样的事情:| --view-- |

我找不到任何使用ConstraintLayout的方法.我找到的最好的是分别使用app:layout_constraintHorizontal_weight="1"位于完整左侧和右侧的2个假视图,以及app:layout_constraintHorizontal_weight="1.5"我的视图.

有更好的方法吗?

Che*_*amp 28

使用测试版,您可以使用百分比宽度.如果您无法使用测试版,则可以采用两个垂直指南:一个占屏幕宽度的25%,另一个占宽度的75%.宽度为的视图0dp将在这两个准则之间受到限制.此设置将为您提供屏幕宽度的1/2并且也居中的视图.

以下XML演示了两种方式; 一个使用ConstraintLayoutbeta版本,另一个使用当前生产版本中提供的功能.

在此输入图像描述 XML布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main_inference"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <View
        android:id="@+id/viewTop"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5" />


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

    <View
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintEnd_toStartOf="@id/guidelineRight"
        app:layout_constraintStart_toEndOf="@id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@id/viewTop" />

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

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)


sha*_*une 18

从"ConstraintLayout1.1.0-beta1"开始,您可以使用百分比来定义宽度和高度.

android:layout_width="0dp"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".4"
Run Code Online (Sandbox Code Playgroud)

这将将with定义为屏幕宽度的40%.百分比和此指南的组合允许您创建所需的任何基于百分比的布局.


Kar*_*lli 5

试试这个垂直分割

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/clPart1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/clPart2">


        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/clPart2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/clPart1"
            app:layout_constraintRight_toRightOf="parent">


        </android.support.constraint.ConstraintLayout>
    </android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)