百分比未按预期工作的约束布局
Constraint Layout with percentage not working as expected
我会制作一个宽度为 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>
TextView 始终占据整个父级宽度。知道我做错了什么吗?
两个小但重要的变化:
- TextView 宽度应为 0dp,即匹配约束而不匹配父项
- 参考线方向应该是垂直的而不是水平的
代码如下:
<?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>
输出:
另请注意,我将 ConstraintLayout 高度更改为 match_parent,以便在输出中可以看到辅助线。您可以将其改回 wrap_content.
我会制作一个宽度为 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>
TextView 始终占据整个父级宽度。知道我做错了什么吗?
两个小但重要的变化:
- TextView 宽度应为 0dp,即匹配约束而不匹配父项
- 参考线方向应该是垂直的而不是水平的
代码如下:
<?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>
输出:
另请注意,我将 ConstraintLayout 高度更改为 match_parent,以便在输出中可以看到辅助线。您可以将其改回 wrap_content.