There is a layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Текст1 Текст1 Текст1 Текст1" android:maxWidth="280dp" android:textSize="22dp"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Текст2 Текст2" android:textSize="20dp"/> </LinearLayout> 

Here is what it looks like:

enter image description here

textView1 takes up more space than it needs (there is free space on the right) and textView2 will not exactly fit in one line, although it could have tried. How to specify textView1 to occupy the width that only it uses?

UPD

Here’s what the screen should look like if textView1 takes up a bit of space, so using android:layout_weight won't work for different screen densities:

enter image description here

  • Add another picture as it should to look great text. - Yuriy SPb

1 answer 1

This is due to the android:maxWidth attribute. Since the size in width is set to wrap_content , the widget tries to fit the entire text as wide as possible and in your case the width of the widget thus turns out to be more than the specified value maxWidth ="280dp" , therefore it is limited to the value you specified in 280dp .

That is, the calculated width for this text is greater than the specified maximum width and the width for the widget is taken as the maximum allowed, and the place remains because of the transfer by whole words, and not syllables.

To solve this issue the way you want (the limit on the size of the occupied place at the place of transfer, if the transfer is closer to the current size) is impossible for a standard widget.

I note that for proportional division of the screen it is better to use weights ( android:layout_weight ) since specifying the hardcore width in terms of density will lead to the fact that on different devices the marking will not look the same due to the fact that different screens have different densities - on the screen hdpi it will be almost the entire width, and on xxhdpi a little more than half.

  • Thanks for the answer. Regarding the use of android: layout_weight, it will not work for me, since as if textView1 is a little wide (for example, one word), then textView2 should be located immediately to its right. - iamtihonov
  • But using android: maxWidth = densities for different screens will indeed incorrectly display, then it turns out you must probably use the value android: maxWidth for different densities. - iamtihonov