英文:
Expand two TextViews next to each other
问题
Sure, here's the translation of the content you provided:
我想将两个 TextView 放在一起,并让它们根据内容扩展。
两个 TextView 的内容都是完全动态和不可预测的,因此我希望解决方案在以下所有情况下都能一致工作,如下图所示:
- 第一个 TextView 的内容非常长(并且可能需要换行成多行),而另一个 TextView 的内容相对较短。
- 恰恰相反,即第一个 TextView 的内容较短。
- 两个 TextView 的内容都很长并且必须换行。理想情况下,它们应该在中间相遇。
我尝试了LinearLayout
、ConstraintLayout
和 RelativeLayout
的各种设置,但在所有情况下都没有一致工作。为了创建上面截图中的布局,我不得不根据每个 TextView 的文本长度手动自定义属性。但是,我假设我不会轻松判断每个文本在运行时需要多少空间(例如,是否需要换行等),因此我担心我不能根据内容动态微调 TextView 的属性。这就是为什么我试图找到一些设置,可以在没有我的干预的情况下一致工作,尽管我知道这可能是不可能的。无论如何,我希望我只是错过了一些明显的东西。
或者,作为最后的手段,我会考虑“测量”每个文本的预期宽度,以决定两个 TextView 的设置并动态微调它们。
英文:
I would like to put two TextViews next to each other and let both of them expand depending on their contents.
The contents of both TextViews are fully dynamic and unpredictable, therefore I want the solution to work consistently in all the cases shown in the image below:
- The first TextView's content is very long (and it may also turn out it must be wrapped into multiple lines), while the other TextView's content is relatively short.
- Exactly the opposite, i.e. the first TextView's content is shorter.
- Both TextViews' contents are long and must be wrapped. Ideally, they should meet in the middle.
I tried various settings of LinearLayout
, ConstraintLayout
and RelativeLayout
but none of them worked consistently in all the cases. To create the layout you can see in the screenshot above, I had to manually customize the attributes of each TextView depending on its text length. However, I presume I won't be able to easily judge how much space each text is going to take in runtime (e.g. whether it will be wrapped into multiple lines etc.), therefore I'm afraid I can't dynamically fine-tune the properties of the TextViews depending on their contents. That's why I'm trying to find some setting that would work consistently without my intervention, although I'm aware it may turn out it's impossible. Anyway, I hope I simply missed something obvious.
Alternatively, I would consider "measuring" the expected width of each text to make a decision regarding both TextViews' settings and fine-tune them dynamically, but only as a last resort.
答案1
得分: 2
你可以尝试使用 Flexbox 来实现上述结果。
查看Flexbox提供的所有参数,以根据您的需求进行调整,例如 app:layout_minWidth
,它提供了子元素的最小宽度。
英文:
You can try to achieve above result using Flexbox
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout 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"
android:padding="8dp"
app:flexDirection="row">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="@tools:sample/lorem/random" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="@tools:sample/lorem/random" />
</com.google.android.flexbox.FlexboxLayout>
Take a look at all parameters that Flexbox provides to adjust to your needs. e.g. app:layout_minWidth
which provides the min width for children.
答案2
得分: 0
将两个文本视图都设置为0dp
。如果您使用Constraint layout
,则将两个视图的边缘约束到它们自身和父视图。您应该会得到您期望的结果。
英文:
Set both text views to 0dp
. And if you're using Constraint layout
, constrain the sides of both views to themselves and to parent. You should get your desired result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论