mambo
mambo

Reputation: 35

Android weightsum not working as expected

I have one horizontal Linear Layout with Width = match_parent and weightsum=5. If i insert 5 vertical Linear Layouts with each width=0 and weight=1 everything looks as expected, the layouts each get the same width. If i add only 2 vertical with each width=0 and weight=1 they take more space than they should. I expected them to also take 1/5 of the space.

Maybe it is the correct behaviour that they take more space and I understood the concept of weight/weightsum wrong.

Thanks for any help!

edit: I try to add some Code

LinearLayout linear=null;
            LinearLayout.LayoutParams layoutParams= new 
            		LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
            		LinearLayout.LayoutParams.WRAP_CONTENT);

linear=new LinearLayout(getApplicationContext());
            		linear.setOrientation(LinearLayout.HORIZONTAL);
            		linear.setLayoutParams(layoutParams);
            		linear.setPadding(15, 0, 15, 10);
            		linear.setWeightSum(Float.valueOf(modulo));
//modulo 5 in my example


LinearLayout linear2=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 
                		LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
if(count%modulo!=modulo-1){
        lp1.setMargins(0, 0, 15, 0);
} else {
        lp1.setMargins(0, 0, 0, 0);
       }
linear2.setLayoutParams(lp1);
linear2.setOrientation(LinearLayout.VERTICAL);

I add the layout linear 2 into linear in a loop Why can you click run code :D

Upvotes: 2

Views: 1744

Answers (3)

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

If the logical solution does not work fill the rest with

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="3"/> <!-- or whatever the rest is -->

or

View space = new View(this); // NEVER CREATE VIEWS WITH APP CONTEXT!
LinearLayout.LayoutParams spaceParams = new LinearLayout.LayoutParams(0, 0, 3f);
linear.addView(space, spaceParams);

NOTE: Space widget didn't work in this case.

Upvotes: 0

Muhammad Umair
Muhammad Umair

Reputation: 593

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="Your bg"
    android:layout_weight="2.5"/>

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="Your bg"
    android:layout_weight="2.5"/>
</LinearLayout>

divide weight equally.

Upvotes: 0

Tarun Tak
Tarun Tak

Reputation: 421

Try this

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    android:layout_weight="1"/>

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:layout_weight="1"/>
</LinearLayout>

Upvotes: 1

Related Questions