在Constraint布局中包装文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Constraint布局中包装文本相关的知识,希望对你有一定的参考价值。
尝试使用ConstraintLayout为设置页面制作一个非常简单的布局。左侧的简单文本视图位于另一个之下,并且向右侧切换到中心。我的布局适用于较新的设备,但是一旦切换到Nexus 4或更早版本,开关就会从视图中消失/消失。
这是我的布局代码,
<android.support.constraint.ConstraintLayout
android:id="@+id/locationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="parent">
<TextView
android:id="@+id/locationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/location_title"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/guidelineLocationLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="16sp"/>
<TextView
android:id="@+id/locationDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/location_description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationTitle"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@id/locationTitle"
app:layout_constraintRight_toLeftOf="@id/guidelineLocationLayout"
android:textSize="12sp"
/>
<Switch
android:id="@+id/locationPermissionSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guidelineLocationLayout"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"/>
<View
android:id="@+id/viewLocationLayout"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#d6d6d6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationDescription"/>
<android.support.constraint.Guideline
android:id="@+id/guidelineLocationLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="48dp" />
</android.support.constraint.ConstraintLayout>
以下是Pixel 2XL的约束效果,
以下是他们在较小的设备上的外观,
我已经使用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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/locationLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="parent">
<TextView
android:id="@+id/locationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Save location to the song"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/locationDescription"
android:layout_width="261dp"
android:layout_height="32dp"
android:text="Your location is only requested when a new song is identified. Location details are never sent to the server"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationTitle" />
<android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="locationDescription,locationTitle" />
<Switch
android:id="@+id/locationPermissionSwitch"
android:layout_width="41dp"
android:layout_height="26dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/barrier"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/viewLocationLayout"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#d6d6d6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/locationDescription" />
</android.support.constraint.ConstraintLayout>
另一答案
您需要像这样更改开关的约束
<Switch
android:id="@+id/locationPermissionSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guidelineLocationLayout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
您将开关的底部约束到父级的顶部,将开关的顶部约束到父级的底部。你必须把它改成layout_constraintTop_toTopOf
和layout_constraintBottom_toBottomOf
以上是关于在Constraint布局中包装文本的主要内容,如果未能解决你的问题,请参考以下文章