Question

I have a LinearLayout (let's call it A)that it's width set to fill_parent, this layout contains a bunch of another LinearLayouts(let's call them B), I want the screen only display four B's so i assigned the weight_sum of A as 4 and assigned each one of B's as 1, now i want to add a HorizontalScrollView so that if i have like 6 layouts of B only four will be displayed and the other two will be scrolled. I build this HorizontalScrollView to contain the A layout(as the HorizontalScrollView should has only one direct child), now the fill_parent width i add to the layout A is ruined as it now obeys the HorizontalScrollView so the B layouts width is ruined also, take a look at the following figures:

-- yellow : the Whole Screen.

-- green : A Layout.

-- red : B Layout.

The result I got:

enter image description here

The result i suppose to get:

enter image description here

I'm tried to set the width of both HorizontalScrollView and the Layout A to fill_parent and/or wrap_content but nothing works with me.

My XML Code:

      <HorizontalScrollView 
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" >
         <LinearLayout 
                       android:id="@+id/A"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:orientation="horizontal"
                       android:weightSum="4" >

            <LinearLayout
                    android:id="@+id/B1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <LinearLayout
                        android:id="@+id/icon1"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/icon2"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>
                </LinearLayout>
             <LinearLayout
                    android:id="@+id/B2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <LinearLayout
                        android:id="@+id/icon3"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/icon4"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>
                  </LinearLayout>
                 </LinearLayout>
    </HorizontalScrollView>
Was it helpful?

Solution

You're not going to be able to accomplish this from xml only, you'll need some dynamic code to measure the width of the screen and then programtically set the width of each linearlayout (icon1, icon2 etc) to 1/4 of this width.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, LayoutParams.MATCH_PARENT);

LinearLayout icon1 = (LinearLayout) findViewById(R.id.icon1);
icon1.setLayoutParams(params);

//etc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top