简体   繁体   English

如何创建这样的布局?

[英]How to create layout like this?

I have a horizontal LinearLayout, inside which I have 2 TextViews. 我有一个水平LinearLayout,其中有2个TextViews。 Let's say that the LinearLayout's width is 320px. 假设LinearLayout的宽度为320px。 If the TextViews don't fit into the LinearLayout (they are together wider than 320px), I want to somehow achieve this: 如果TextViews不适合LinearLayout(它们在一起的宽度大于320px),我想以某种方式实现这一点:

  • The second TextView is fully displayed and is at the right edge of the LinearLayout 第二个 TextView已完全显示,并位于LinearLayout的右侧
  • The first TextView is only shown partially, only first x characters are visible 一个 TextView仅部分显示,仅前x个字符可见

What I mean: 我的意思是:

[TextView1|TextView2_________________________] // this is normal [TextView1 | TextView2 _________________________] //这很正常

[VeryVeryL...|VeryVeryLongTextView2] // VeryVeryLongTextView1 is not fully visible [VeryVeryL ... | VeryVeryLongTextView2] // VeryVeryLongTextView1并不完全可见

Specify a specific width for your first textView (ie, 20dp... note, it is better to use dp than hard coded pixels, to deal with multiple resolutions of devices), give your 2nd TextView a weight of 1. This tells it to take up the remaining space. 为您的第一个TextView指定一个特定的宽度(即20dp ...注意,使用dp比处理硬编码像素更好,以处理多种分辨率的设备),给您的第二个TextView分配1的权重。占用剩余空间。 For example: 例如:

<LinearLayout ...>
    <TextView android:layout_width="20dp"
                android:layout_height="wrap_content"/>
    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
</LinearLayout>

To get the effect you're requesting in the comments above, you could modify Mayra's solution to something like: 为了获得您在上面的评论中要求的效果,您可以将Mayra的解决方案修改为以下内容:

<LinearLayout ...>
<TextView android:layout_width="wrap_content"
            android:maxWidth="20dp"
            android:layout_height="wrap_content"/>
<TextView android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

I think that will work. 我认为这行得通。 Weirdly, the maxWidth param is only present on a couple view classes, but TextView luckily is one of them. 奇怪的是,maxWidth参数仅出现在几个视图类中,但幸运的是TextView是其中之一。 You'd think it'd be useful in more cases, so I'm not sure why it's not just available in the default view params. 您可能会认为它在更多情况下很有用,所以我不确定为什么它不仅在默认视图参数中可用。

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

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