Android sets maximum width as a percentage of TextView in LinearLayout vertical orientation

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>
+4
3

. , , .
PercentRelativeLayout com.android.support:percent:25.1.0 .

 <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>
+4

android:weightSum View, :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="10"
            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="match_parent"
    android:weightSum="10">
        <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="8"
            android:textStyle="bold"
            tools:text="a pretty long text a pretty long text a pretty long text" />
    </LinearLayout>
</LinearLayout>
+1

android: layout_weight

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"/>
    </LinearLayout>

TRY TextView 75% . SPACER , , 2 50%.

0

Source: https://habr.com/ru/post/1667791/


All Articles