How to set the linear layout width in a HorizontalScrollView?

落爺英雄遲暮 提交于 2019-12-08 07:53:14

问题


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:

The result i suppose to get:

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>

回答1:


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


来源:https://stackoverflow.com/questions/20858958/how-to-set-the-linear-layout-width-in-a-horizontalscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!