首页 > 解决方案 > layout_constraintGuide_percent 不起作用 - android

问题描述

我正在使用ConstraintLayout. 我想将页面分成两种颜色,并且我想在整个屏幕上拥有一个 70% 高度的布局。

这是我的代码:

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




    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:orientation="vertical" >

        <android.support.v7.widget.CardView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/wikitude_primary">

        </android.support.v7.widget.CardView>

        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"></LinearLayout>


    </LinearLayout>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8"/>


</android.support.constraint.ConstraintLayout>

app:layout_constraintGuide_percent不起作用。

我想做出这样的看法:

https://www.pinterest.com/pin/189995678015932713/

标签: javaandroidandroid-constraintlayout

解决方案


您的布局几乎没有问题。最值得注意的是,您没有将约束设置为准则。尝试在 Android Studio 设计器中生成此图像的以下布局。我添加了文本视图来标识这些部分,但其他情况下不需要它们。

在此处输入图像描述

activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:weightSum="2"
        app:layout_constraintBottom_toTopOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:cardBackgroundColor="@color/wikitude_primary">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="CardView"
                android:textSize="24sp"
                android:textStyle="bold" />
        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bottom LinearLayout"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />


</android.support.constraint.ConstraintLayout>

推荐阅读