简体   繁体   English

如何将视图放置在TextView的右侧

[英]How to place a View to the right of a TextView

I would like to place a TextView and another View next to it. 我想在其旁边放置一个TextView和另一个View Like this: 像这样:

|                             |
|[TextView][View]             |
|                             |

The TextView might have a long text. TextView文本可能很长。 In that case, I would like to use ellipsis so the View is not pushed out of the screen (or whatever layout contains them) nor deformed. 在那种情况下,我想使用省略号,这样就不会将View推出屏幕(或包含它们的任何布局)也不会变形。

|                             |
|[TextView with long...][View]|
|                             |

So I want the View to be next to TextView while the TextView takes as much space as needed (and no more than needed) without pushing the View out of the screen/container when the text is too long (in that case, I'd like to cut the text with ellipsis). 因此,我希望ViewTextView旁边,而TextView则根据需要占用尽可能多的空间(且不超过所需空间),而不会在文本过长时将View从屏幕/容器中推出(在这种情况下,我希望喜欢用省略号剪切文本)。

How could I do that? 我该怎么办?

I was thinking of calculating the positions and sizes of the views but seems a bit complicated. 我当时正在考虑计算视图的位置和大小,但似乎有点复杂。 Maybe there's some simpler solution. 也许有一些更简单的解决方案。

Thank you! 谢谢!

You can use an horizontal LinearLayout with width math_parent , which contains two children. 您可以使用宽度为math_parent的水平LinearLayout ,其中包含两个子math_parent The left TextView will have 0dp of width and weight 1, the View will have wrap_content for the width. 左边的TextView的宽度为0dpweight 1,View的宽度为wrap_content For the dots at the end you can use android:ellipsize 对于末尾的点,您可以使用android:ellipsize

You can use a LinearLayout with horizontal orientation as blackbelt suggested, but setting its width to wrap_content 您可以按照Blackbelt建议使用水平方向的LinearLayout,但将其宽度设置为wrap_content

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="unknown sized view" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fixed sized view"/>
</LinearLayout>

In the parent layout of TextView and View, set layout_weightsum = 3. 在TextView和View的父布局中,设置layout_weightsum = 3。

In TextView, set layout_weight=2 and in View, set layout_weight=1. 在TextView中,设置layout_weight = 2,在View中,设置layout_weight = 1。

Irrespective of the length of the text in textview, view and textview will be rendered properly. 无论textview中文本的长度如何,view和textview都将正确呈现。

根据developer.android.com上的RelativeLayout.LayoutParams ,可以将RelativeLayoutandroid:layout_toRightOf ,这与您一直在寻找的东西完全相同。

you could try putting both views inside a horizontal LinearLayout and setting android:maxWidth for the TextView, that way the View will remain on the right side of the TextView when it's short 您可以尝试将两个视图都放在水平LinearLayout内,并为TextView设置android:maxWidth ,这样,当视图变短时,视图将保留在TextView的右侧

|[TextView short][View]_______| | [TextView短] [查看] _______ |

, and when it's long, the TextView will only expand as much as you set its maximum width. ,而且很长时,TextView只会扩展到您设置的最大宽度。

|[TextView with long t...][View]| | [具有较长时间的TextView ...] [查看] |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM