I would like to set layout_weight TextView with tv_long_text to 80% in the next LinearLayout vertical orientation.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<TextView
android:id="@+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
tools:text="a pretty long text" />
</LinearLayout>
The above does not work because the orientation of the textview parent is vertical.
So, I tried setting android:layout_width="match_parent"in xml and then setting the width at runtime, getting the measured width , and then setting the width to 80%, but getMeasuredWidthgiving me 0.
int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);
layout_weight , , , , , .
longTextView.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT,
0.8f)
);
, , . 2 , . ?
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
android:textStyle="bold"
tools:text="a pretty long text" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
</LinearLayout>
</LinearLayout>