使用 layout_constraintVertical_weight 分发 TextView
Distributing TextViews with layout_constraintVertical_weight
我在使用 layout_constraintVertical_weight
时遇到了一个小问题,我正在尝试让两个 TextView 共享分配区域中可用垂直 space 的数量,但如果没有我手动分配,这不会发生。我尝试为高度添加 0dp
但这不起作用,但是它完美地用于宽度。这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<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="@dimen/list_item_height"
android:background="@color/tan_background">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
android:layout_height="44dp"
android:background="@color/category_colors"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
android:layout_height="44dp"
android:background="@color/category_numbers"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_weight="1"
tools:text="text2" />
</android.support.constraint.ConstraintLayout>
这是布局外观的 img:https://i.stack.imgur.com/ZOs9y.png
我设法按照指南完成了它。这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<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="@dimen/list_item_height"
android:background="@color/tan_background">
<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.5" />
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/category_colors"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/category_numbers"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/miwok_text_view"
tools:text="text2" />
</android.support.constraint.ConstraintLayout>
不确定这是最好的方法,但确实有效:)
您必须创建一个 layout_height 为 0dp 的链才能工作
为了创建一个链,链中的所有视图都必须包含对该链中前一个和下一个项目的约束,并且第一个和最后一个必须与父级对齐
在你的情况下,为了让它工作,你必须做这样的事情:
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
--this should be 0dp otherwise it will not use weight
android:layout_height="0dp"
android:background="@color/category_colors"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
---this is what you have to add--
app:layout_constraintBottom_toTopOf="@+id/default_text_view"
app:layout_constraintVertical_weight="1"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
-- again 0dp otherwise weight is ignored
android:layout_height="0dp"
android:background="@color/category_numbers"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
-- again add this to create a chain
app:layout_constraintTop_toBottomOf="@+id/miwok_text_view"
app:layout_constraintVertical_weight="1"
tools:text="text2" />
经过上述更改权重后应该可以正常工作
我在使用 layout_constraintVertical_weight
时遇到了一个小问题,我正在尝试让两个 TextView 共享分配区域中可用垂直 space 的数量,但如果没有我手动分配,这不会发生。我尝试为高度添加 0dp
但这不起作用,但是它完美地用于宽度。这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<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="@dimen/list_item_height"
android:background="@color/tan_background">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
android:layout_height="44dp"
android:background="@color/category_colors"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
android:layout_height="44dp"
android:background="@color/category_numbers"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_weight="1"
tools:text="text2" />
</android.support.constraint.ConstraintLayout>
这是布局外观的 img:https://i.stack.imgur.com/ZOs9y.png
我设法按照指南完成了它。这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<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="@dimen/list_item_height"
android:background="@color/tan_background">
<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.5" />
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/category_colors"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/category_numbers"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/miwok_text_view"
tools:text="text2" />
</android.support.constraint.ConstraintLayout>
不确定这是最好的方法,但确实有效:)
您必须创建一个 layout_height 为 0dp 的链才能工作
为了创建一个链,链中的所有视图都必须包含对该链中前一个和下一个项目的约束,并且第一个和最后一个必须与父级对齐
在你的情况下,为了让它工作,你必须做这样的事情:
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="0dp"
--this should be 0dp otherwise it will not use weight
android:layout_height="0dp"
android:background="@color/category_colors"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
---this is what you have to add--
app:layout_constraintBottom_toTopOf="@+id/default_text_view"
app:layout_constraintVertical_weight="1"
tools:text="text1" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="0dp"
-- again 0dp otherwise weight is ignored
android:layout_height="0dp"
android:background="@color/category_numbers"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
-- again add this to create a chain
app:layout_constraintTop_toBottomOf="@+id/miwok_text_view"
app:layout_constraintVertical_weight="1"
tools:text="text2" />
经过上述更改权重后应该可以正常工作