Вопрос

I want to get my LinearLayout to display views in the desired proportion. I have perused some 10 or 15 other questions about LinearLayout and layout_weight, but for some reason no answers seem to apply in my case. I am creating a custom AlertDialog color-picker with a custom View created as follows:

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;
}

The problem I'm having is that despite having set the layout_height of my three vertical elements (PresetView, subLayout, and LineView) to 0, the weight is still not distributing the available space how I want. subLayout does not get 70% of the space, but always less.

From what I can tell, the PresetView is always getting as much vertical space as it asks for, and subLayout and LineView are left to take up the remaining. Here is my onMeasure() method for PresetView:

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

The 250 and 150 are rather arbitrary values - the PresetView is designed to work with just about any set of dimensions (because I want the LinearLayout to control its size!).

I was under the impression that using a layout_height of 0 and specifying a weight put the layout in control of the space taken up by the View, rather than the View controlling it. Why is the PresetView still taking up a full 150 pixels, when I set it's layout_height to 0?

The onMeasure() method of the other Views is identical to PresetView's, only with different values.

Это было полезно?

Решение

It looks like I was overriding onMeasure() improperly. The route that I took is useful if the View has predefined measurements (you KNOW that it will be 250 x 150 pixels). However, since I wanted my Views to scale to fit any screen size and be controlled completely by their layouts, all that I needed to do was use the default onMeasure() implementation:

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

This allows the layout_weight attribute to handle all sizing of the child Views.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top