Android 具有两个链接文本视图的约束布局不';渲染不好
我正在尝试创建一个布局,它将两个Android 具有两个链接文本视图的约束布局不';渲染不好,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我正在尝试创建一个布局,它将两个文本视图打包在一起,如图片#1中所示。现在,当第一个(红色)TextView的文本超过允许的空间时,它会自行扩展以适应其约束条件,这是一种预期行为,请参见picture\2 现在,当我扭转这种情况时,让第二个(紫色)文本视图展开,我没有得到类似的结果-picture\3。我们可以看到,第二个TextView不遵守第一个TextView约束 我还发现的另一件事是,如果我们将*.xml文件中TextView的顺序替换为“TextView red->TextView v
文本视图
打包在一起,如图片#1
中所示。现在,当第一个(红色)TextView
的文本超过允许的空间时,它会自行扩展以适应其约束条件,这是一种预期行为,请参见picture\2
现在,当我扭转这种情况时,让第二个(紫色)文本视图展开,我没有得到类似的结果-picture\3
。我们可以看到,第二个TextView
不遵守第一个TextView
约束
我还发现的另一件事是,如果我们将
*.xml
文件中TextView
的顺序替换为“TextView red->TextView violet”到“TextView violetEdit:被误解的通缉行为”。
你有两个选择,使用
app:layout_constraintHeight_min="minimumHeight"
或
app:layout\u constraintHeight\u percent=“percentage/100”您需要指定最小高度约束
app:layout\u constraintHeight\u min=“wrap”“
发送到两个TextView
s,以确保两个视图至少都会包装其内容,换句话说,这将使任何TextView
s不会互相贪婪
这也将保持两个文本视图
唯一的缺点是TextView
s不遵守高度限制,您可以在下面的pic高度限制虚线中看到,顶部的TextView
text也被剪切。这是因为textview
都倾向于在保留约束的同时包装其内容
在我的试验中,填充与TextView
的大小以及两者的内容大小成正比
在顶部TextView
顶部和底部Textivew
另一种处理方法是严格遵守高度约束,不要通过从两者中删除app:layout\u constraintHeight\u default=“wrap”
来包装高度内容
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f00"
android:paddingTop="0dp"
android:text="a"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="@+id/b"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/b"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0f"
android:paddingTop="0dp"
android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbzzzzzzzzzzzzzzz"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/a" />
</androidx.constraintlayout.widget.ConstraintLayout>
我认为这只是ConstraintLayout
的一个限制。当有两个视图比容器大时,其中一个视图必须被另一个视图遮挡。ConstraintLayout似乎优先于最后定义的视图。也许你可以使用app:layout\u constraintheighth\u max
来解决这个问题?是的,我的案例可能需要使用最小高度。而不是我想要达到的行为。当文本较小时,应将其堆叠在一起。您可以使用then app:layout\u ConstraintheRight\u percent=“percentage/100”使文本视图占据屏幕的一部分。设置最小高度确实可行,但这只是一种解决方法。我可能会把它作为最后的手段。这可能会给你的第二个解决方案带来一些线索,让你找到更好的解决方案。我已经在用android:gravity=“bottom”为top TextView尝试了这个方法,以获得更符合我需要的东西。第一个添加填充/边距的解决方案即使对我来说也有点粗糙。
app:layout_constraintHeight_min="minimumHeight"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f00"
android:paddingTop="0dp"
android:text="a"
android:textSize="50sp"
app:layout_constraintBottom_toTopOf="@+id/b"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/b"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#f0f"
android:paddingTop="0dp"
android:text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbzzzzzzzzzzzzzzz"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/a" />
</androidx.constraintlayout.widget.ConstraintLayout>