约束布局,百分比无法按预期工作

Bao*_* Le 3 android android-constraintlayout

我会创建一个宽度为70%的视图,并使用约束布局对齐其父级的右侧,如下所示

<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.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Hello text"
        app:layout_constraintLeft_toRightOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"/>

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

TextView始终占用完整的父宽度.知道我做错了什么吗?

Roh*_*eja 7

两个小但重要的变化:

  1. TextView宽度应为0dp,即匹配约束,不匹配父级
  2. 指导方向应垂直而非水平

这是代码:

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

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello text"
        app:layout_constraintLeft_toRightOf="@id/guideline"
        app:layout_constraintRight_toRightOf="parent" />

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

输出:

在此输入图像描述

另请注意,我将ConstraintLayout高度更改为match_parent,以便在输出中显示指南.您可以将其更改回wrap_content.