The last word in the last line of Android may lead to an incorrect text comment size when using the correct field

I have strange behavior when using live text in text form.

So, the problem is this: I created a custom element that contains text and should be used as an element later in the list, so the element is contained several times in the linear scroll scrollviews, which has a vertical orientation.

arrangement of elements is as follows

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/ButtonItem"> <TextView android:id="@+id/m_oItem_TextView_TopLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="20dp" android:layout_alignParentTop="true" android:layout_marginTop="10dp" android:textColor="@color/black" android:textSize="@dimen/font_size_12" /> <TextView android:id="@+id/m_oItem_TextView_TopMiddle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft" android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight" android:layout_marginRight="20dp" android:textColor="@color/black" android:textSize="@dimen/font_size_12" android:gravity="right"/> <TextView android:id="@+id/m_oItem_TextView_TopRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="40dp" android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" android:textColor="@color/black" android:textSize="@dimen/font_size_12"/> <TextView android:id="@+id/m_oItem_TextView_Bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" android:layout_alignRight="@+id/m_oItem_TextView_TopRight" android:layout_below="@+id/m_oItem_TextView_TopLeft" android:layout_marginTop="5dp" android:textColor="@color/black" android:textSize="@dimen/font_size_14" /> <ImageView android:id="@+id/m_oItem_PlaceHolder" android:layout_width="wrap_content" android:layout_height="10dp" android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" android:layout_alignRight="@+id/m_oItem_TextView_TopRight" android:layout_below="@+id/m_oItem_TextView_Bottom" android:contentDescription="@string/image_description"/> <ImageView android:id="@+id/m_oPart_Arrow_Image" android:scaleType="fitCenter" android:layout_width="20dp" android:layout_height="20dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="@drawable/arrow_right" android:contentDescription="@string/image_description"/> </RelativeLayout> 

text m_oItem_TextView_Bottom is the part that will contain text that may be too long for it, thereby increasing the height of the text field, if necessary. When I tested this, everything worked correctly, and the text image had the required height.

however, as soon as I installed

 android:textStyle="bold" 

the text, of course, will become wider, so more lines may be required. Now the problem is that the android will somehow not be counted.

eg. if not a bold line barely requires two lines (the last element in the image below)

last item with two lines

bold text will require three lines, but the text image will still only contain two lines

last item with three lines

Has anyone ever encountered the same problem or had a solution, or at least a suggestion on how I can solve the problem. Thanks in advance anyway

EDIT: as requested by ButtonItem style, this is

 <style name="ButtonItem"> <item name="android:background">@drawable/button_item</item> </style> 

@ drawable / button_item:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_item_down" /> <item android:state_selected="true" android:drawable="@drawable/button_item_selected" /> <item android:drawable="@drawable/button_item_normal" /> </selector> 

@ drawable / button_item_down:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:bottom="1dp" android:left="-2dp" android:right="-2dp" android:top="-2dp"> <shape android:shape="rectangle"> <solid android:color="@color/white" /> <stroke android:width="@dimen/button_item_stroke_thickness" android:color="@color/black"/> </shape> </item> </layer-list> 

@ drawable / button_item_selected:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:bottom="1dp" android:left="-2dp" android:right="-2dp" android:top="-2dp"> <shape android:shape="rectangle"> <solid android:color="@color/white" /> <stroke android:width="@dimen/button_item_stroke_thickness" android:color="@color/black"/> </shape> </item> </layer-list> 

@ drawable / button_item_normal:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:bottom="1dp" android:left="-2dp" android:right="-2dp" android:top="-2dp"> <shape android:shape="rectangle" > <solid android:color="@color/transparent" /> <stroke android:width="@dimen/button_item_stroke_thickness" android:color="@color/black" /> </shape> </item> </layer-list> 

essentially they are there to create a black line below the element and make a background white if it is clicked or selected. although the selected value is never used, since the selected state is never set in the code.

I also tried it without a background style, but as expected, the problem remained the same.

EDIT AGAIN: Okay, so I managed to track the problem even more, resizing the text is actually correct. The specific problem is as follows. If the last word of a line (for example, "this is a test" and the test is the last word) causes a line break, the last word should be long enough. I am testing this using dummy strings with the same element that is used in the question.

here are some photos enter image description here

As you can see, the line that causes the problem requires another character, then the word is long enough to fit.

LAST EDITOR: okay found the answer (see below)

+4
source share
2 answers

FINALLY I MARKED THE NUMBER,

So, here is the solution, the problem was margin_Right from m_oItem_TextViewBottom, which was called:

 android:layout_alignRight="@+id/m_oItem_TextView_TopRight" 

so he effectively created the right margin, which was a troublemaker, I tested this with other texts and right margins, and they all had the same problem.

I replaced it with a fill with the same difference and ended up with this final solution for the item.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/ButtonItem"> <TextView android:id="@+id/m_oItem_TextView_TopLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="20dp" android:layout_alignParentTop="true" android:layout_marginTop="10dp" android:textColor="@color/black" android:textSize="@dimen/font_size_12" /> <TextView android:id="@+id/m_oItem_TextView_TopMiddle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft" android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight" android:layout_marginRight="20dp" android:textColor="@color/black" android:textSize="@dimen/font_size_12" android:gravity="right"/> <TextView android:id="@+id/m_oItem_TextView_TopRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:paddingRight="40dp" android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" android:textColor="@color/black" android:textSize="@dimen/font_size_12"/> <TextView android:id="@+id/m_oItem_TextView_Bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" android:layout_below="@+id/m_oItem_TextView_TopLeft" android:layout_marginTop="5dp" android:paddingRight="40dp" android:textColor="@color/black" android:textStyle="bold" /> <ImageView android:id="@+id/m_oItem_PlaceHolder" android:layout_width="wrap_content" android:layout_height="10dp" android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" android:layout_alignRight="@+id/m_oItem_TextView_TopRight" android:layout_below="@+id/m_oItem_TextView_Bottom" android:contentDescription="@string/image_description"/> <ImageView android:id="@+id/m_oPart_Arrow_Image" android:scaleType="fitCenter" android:layout_width="20dp" android:layout_height="20dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="@drawable/arrow_right" android:contentDescription="@string/image_description"/> </RelativeLayout> 
0
source

It’s good that the TextView is great for bold text, so its RelativeLayout needs to be expanded to fit the extended TextView. You may be able to use LinearLayouts (one vertical and possibly the other horizontally, corresponding to the design you created with RelativeLayout), because they worked well when changing their children.

Link: http://logc.at/2011/10/18/when-to-use-linearlayout-vs-relativelayout/

+1
source

All Articles