如何使用约束布局将屏幕分成两个相等的部分?

问题描述 投票:0回答:1

我想使用约束布局将屏幕分成两半?我只能找到相对和线性布局的示例,有人可以帮助我吗?我附上了屏幕截图,以显示我的外观。Image attached

android android-constraintlayout
1个回答
0
投票

最佳方法是使用<GuideLine />

<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="match_parent">

    <android.support.constraint.Guideline
            android:layout_width="1dp"
            android:layout_height="1dp"
            android:id="@+id/guideline"
            app:layout_constraintGuide_percent="50%"
            android:orientation="horizontal"/>

    <TextView
            android:text="Text 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:gravity="center"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/guideline" />

    <TextView
            android:text="Text 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:gravity="center"
            app:layout_constraintBottom_toTopOf="@+id/guideline"
            app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

有关android约束布局准则的更多信息,您可以阅读其官方文档here

[Here's另一篇很棒的文章,学习如何使用准则

© www.soinside.com 2019 - 2024. All rights reserved.