How to set textview wrap_content width but limit 1/3 of parent width

Having a TextView , its width should not exceed 1/3 of its parent width. If its width is less than 1/3 of the parent, then it should have the wrap_content behavior. His horizontal sibling will always be by his side.

Tried to follow, it always has a hard cut 1/3 and 2/3, therefore, if text1 has less space than 1/3, then two TextView will not start next to it.

change the value of LinearLayout to RelativeLayout , then android:layout_weight="n" does not work

in principle, you need to determine the width of wrap_content , and maxWidth will not exceed 1/3.

any suggestion?

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_width="0dp" android:singleLine="true" android:ellipsize="end" android:layout_weight="2" /> </LinearLayout> 
+7
android textview android-layout-weight
source share
4 answers

I think that you will have to dynamically check the width of the parent layout every time you update textView, something like (I tested this code with a button and edited the text to change the textView - it works without problems):

 <LinearLayout android:id="@+id/myLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tvA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" /> <TextView android:id="@+id/tvB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" /> </LinearLayout> 

the code:

  TextView tvA = (TextView) findViewById(R.id.tvA); TextView tvB = (TextView) findViewById(R.id.tvB); LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout); // code to use when the textView is updated // possibly button onClickListener? tvA.measure(0, 0); int textWidth = tvA.getMeasuredWidth(); myLayout.measure(0,0); int layoutWidth = myLayout.getWidth(); if (textWidth > (layoutWidth / 3)) { tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f)); tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f)); } else { tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); } 
+3
source share

in principle, you need to determine the width - wrap_content, and maxWidth does not exceed 1/3.

If that's all you need, then my suggestion is to abandon the Weight approach and dynamically set the TextView to maxWidth .

Something like that:

 tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3); 
+3
source share

The solution is simple: your second Textview width should be "0dp" as the first.

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="3"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" android:layout_weight="2" /> </LinearLayout> 
0
source share

How to place it in a layout with 1/3 of the width of the parent element?

 <LinearLayout .. this is main parent android:weightSum="1" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.33" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> 
0
source share

All Articles