使用嵌套的LinearLayout时,Layout_weight无法正常工作

时间:2012-04-13 04:00:12

标签: android android-layout android-linearlayout android-layout-weight

我想让LinearLayout以所需的比例显示视图。我已经仔细阅读了有关LinearLayout和layout_weight的10或15个其他问题,但由于某种原因,似乎没有答案适用于我的情况。我正在创建一个自定义AlertDialog颜色选择器,其自定义视图创建如下:

private LinearLayout createLayout() {
    //Create our top-level layout
    LinearLayout mainLayout = new LinearLayout(mContext);
    mainLayout.setOrientation(LinearLayout.VERTICAL);
    mainLayout.setPadding(15, 0, 15, 0);
    //Make the preset view at the top
    LinearLayout.LayoutParams presetViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.2f);
    mainLayout.addView(new PresetView(mContext), presetViewParams);
        //Create another linear layout
        LinearLayout subLayout = new LinearLayout(mContext);
        subLayout.setOrientation(LinearLayout.HORIZONTAL);
        subLayout.setPadding(0, 15, 0, 15);
        //Add the Sat Lum View to the second layout
        LinearLayout.LayoutParams satLumViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.60f);
        subLayout.addView(new SatLumView(mContext), satLumViewParams);
        //Add the divider between the sat lum view and the hue view
        LinearLayout.LayoutParams dividerViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.1f);
        subLayout.addView(new DividerView(mContext), dividerViewParams);
        //Add the Hue View to the second layout
        LinearLayout.LayoutParams hueViewParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.30f);
        subLayout.addView(new HueView(mContext), hueViewParams);
        //Add the second layout to the first layout
        LinearLayout.LayoutParams subLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.7f);
        mainLayout.addView(subLayout, subLayoutParams);
    //Add the Line view at the bottom of the main layout
    LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f);
    mainLayout.addView(new LineView(mContext), lineParams);
    //Return our completed layout
    return mainLayout;
}

我遇到的问题是,尽管已经将我的三个垂直元素(PresetView,subLayout和LineView)的layout_height设置为0,但权重仍然没有按我想要的方式分配可用空间。 subLayout没有获得70%的空间,但总是更少。

据我所知,PresetView总是获得尽可能多的垂直空间,而subLayout和LineView将占用剩下的空间。这是PresetView的onMeasure()方法:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = resolveSize(250, widthMeasureSpec);
        int height = resolveSize(150, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

250和150是相当任意的值 - PresetView设计用于几乎任何维度集(因为我希望LinearLayout控制它的大小!)。

我的印象是使用layout_height为0并指定权重使布局控制View占用的空间,而不是View控制它。当我将它的layout_height设置为0时,为什么PresetView仍然占用了整整150个像素?

其他视图的onMeasure()方法与PresetView相同,只有不同的值。

1 个答案:

答案 0 :(得分:1)

看起来我不正确地压在了测量()上。如果View具有预定义的测量值(您知道它将是250 x 150像素),我所采用的路线非常有用。但是,由于我希望我的视图可以缩放以适应任何屏幕大小并完全由其布局控制,所以我需要做的就是使用默认的onMeasure()实现:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

这允许layout_weight属性处理子视图的所有大小调整。