这是我需要实现的目标:
|TEXTVIEW1|TEXTVIEW2|按钮|
TV1 可以随心所欲地增长,但只占用显示内部文本所需的空间。如果文本大于它所拥有的空间,那么它将变成椭圆形。 TV2 必须始终位于 TV1 文本的右侧,并且必须显示其所有内容。它不能有椭圆形。它总是必须显示其文本。 TV1 和 2 是单线的。 BUTTON固定在屏幕右侧。
示例 1:
TV1 = 这是一个文本太长的文本 TV2 = 1923
|这是带有...1923 按钮的文本|
示例 2:
TV1 = 仅此而已 TV2 = 1923
|就是这个1923________按钮|
(忽略下划线)
我已经尝试了 1000 种不同的权重组合、wrap_content 等......但仍然无法正确组合。
请帮忙!
最佳答案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"*
android:ellipsize"end" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
这是达到您想要的合身效果所需的尺码。在这些 xml 属性之后,您可以添加您想要应用的任何其他样式,例如颜色或文本。我给单行加了星标,因为这会阻止它垂直增长。如果您希望文本在一行中对齐,即使文本小于屏幕,请仅将 android:gravity="right" 添加到 tv1。
关于android - 无法在布局中正确获取 Textviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275623/