简体   繁体   中英

How to create layout like this?

I have a horizontal LinearLayout, inside which I have 2 TextViews. Let's say that the LinearLayout's width is 320px. If the TextViews don't fit into the LinearLayout (they are together wider than 320px), I want to somehow achieve this:

  • The second TextView is fully displayed and is at the right edge of the LinearLayout
  • The first TextView is only shown partially, only first x characters are visible

What I mean:

[TextView1|TextView2_________________________] // this is normal

[VeryVeryL...|VeryVeryLongTextView2] // VeryVeryLongTextView1 is not fully visible

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. 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:

<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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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