如何设置在Horizo​​ntalScrollView线性布局宽度是多少? [英] How to set the linear layout width in a HorizontalScrollView?

查看:137
本文介绍了如何设置在Horizo​​ntalScrollView线性布局宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的LinearLayout (我们称之为的 A ),它的宽度设置为 FILL_PARENT ,这个布局包含了一堆另一个 LinearLayouts (我们称之为他们的),我想要的屏幕只显示的 的,所以我分配 weight_sum A 4 并分配每个人的的如 1 ,现在我想添加一个 Horizo​​ntalScrollView 因此,如果我有这样的6布局的只有四会显示,另外两个将滚动。我建立这个 Horizo​​ntalScrollView 包含在 A 布局(如 Horizo​​ntalScrollView 应该只有一个直接子),现在FILL_PARENT宽度我添加到布局中的 A 是毁了,因为它现在服从 Horizo​​ntalScrollView 这样的布局宽度也毁了,看看下面的数字:

- 的:整个屏幕

- 绿色 A 布局

- 红色的<强>乙布局

我得到的结果是:

结果我想获得:

我试过同时设置宽度 Horizo​​ntalScrollView 和布局的 A FILL_PARENT 和/或 WRAP_CONTENT 但没有任何工程在我身边。

我的 XML code:

 &LT; Horizo​​ntalScrollView
                   机器人:layout_width =WRAP_CONTENT
                   机器人:layout_height =WRAP_CONTENT&GT;
         &LT;的LinearLayout
                       机器人:ID =@ + ID / A
                       机器人:layout_width =FILL_PARENT
                       机器人:layout_height =WRAP_CONTENT
                       机器人:方向=横向
                       机器人:weightSum =4&GT;            &LT;的LinearLayout
                    机器人:ID =@ + ID / B1
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_weight =1
                    机器人:方向=垂直
                    机器人:weightSum =1&GT;                    &LT;的LinearLayout
                        机器人:ID =@ + ID / ICON1
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =0dp
                        机器人:layout_marginLeft =10dp
                        机器人:layout_marginRight =5DP
                        机器人:layout_marginTop =15dp
                        机器人:layout_weight =0.5
                        机器人:背景=@绘制/ ac_overlay
                        机器人:方向=横向
                        机器人:标签=正常&GT;
                    &LT; / LinearLayout中&GT;                    &LT;的LinearLayout
                        机器人:ID =@ + ID /图标2
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =0dp
                        机器人:layout_marginLeft =10dp
                        机器人:layout_marginRight =5DP
                        机器人:layout_marginTop =15dp
                        机器人:layout_weight =0.5
                        机器人:背景=@绘制/ ac_overlay
                        机器人:方向=横向
                        机器人:标签=正常&GT;
                    &LT; / LinearLayout中&GT;
                &LT; / LinearLayout中&GT;
             &LT;的LinearLayout
                    机器人:ID =@ + ID / B2
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_weight =1
                    机器人:方向=垂直
                    机器人:weightSum =1&GT;                    &LT;的LinearLayout
                        机器人:ID =@ + ID / ICON3
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =0dp
                        机器人:layout_marginLeft =10dp
                        机器人:layout_marginRight =5DP
                        机器人:layout_marginTop =15dp
                        机器人:layout_weight =0.5
                        机器人:背景=@绘制/ ac_overlay
                        机器人:方向=横向
                        机器人:标签=正常&GT;
                    &LT; / LinearLayout中&GT;                    &LT;的LinearLayout
                        机器人:ID =@ + ID /图标4
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =0dp
                        机器人:layout_marginLeft =10dp
                        机器人:layout_marginRight =5DP
                        机器人:layout_marginTop =15dp
                        机器人:layout_weight =0.5
                        机器人:背景=@绘制/ ac_overlay
                        机器人:方向=横向
                        机器人:标签=正常&GT;
                    &LT; / LinearLayout中&GT;
                  &LT; / LinearLayout中&GT;
                 &LT; / LinearLayout中&GT;
    &LT; / Horizo​​ntalScrollView&GT;


解决方案

你不会是能够从XML做到这一点而已,你需要一些动态code测量屏幕的宽度和然后程式设计设置每个的LinearLayout(ICON1,图标2等)的宽度,以该宽度的1/4。

  DisplayMetrics指标=新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(指标)。LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(metrics.widthPixels / 4,LayoutParams.MATCH_PARENT);的LinearLayout ICON1 =(的LinearLayout)findViewById(R.id.icon1);
icon1.setLayoutParams(PARAMS);//等等

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>

解决方案

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

这篇关于如何设置在Horizo​​ntalScrollView线性布局宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆