Fajar Nur P
Fajar Nur P

Reputation: 25

How to make layout_weight fix its width

How to make this aligned to left? I want the second column is aligned to left. I using layout_weight but when the content length is diferent it will change the width of the parent view

enter image description here

layout.xml


        <LinearLayout
            ...
            android:weightSum="1.1">

            <FrameLayout
                ...
                android:layout_weight="0.5">

                <TextView/>

            </FrameLayout>

            <FrameLayout
                ...
                android:layout_weight="0.3">

                <TextView/>

            </FrameLayout>

            <FrameLayout
                ...
                android:layout_weight="0.3">

                <TextView/>

            </FrameLayout>

        </LinearLayout>

Upvotes: 0

Views: 57

Answers (1)

KKKKK
KKKKK

Reputation: 284

this is an example of mine:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Music"
        android:textSize="20dp"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="fewwe"
            android:textSize="20dp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="wfeewf"
            android:textSize="20dp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="DasDAS"
            android:textSize="20dp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="dASfafFADS"
            android:textSize="20dp"/>
    </LinearLayout>
</LinearLayout>

and this is the result:

enter image description here

You have to put always the layout_weight=1 and remember the layout_width="match_parent"

I hope this example can help you to solve your problem, cya :)

Upvotes: 1

Related Questions