layout View Auto - Android User Interface

Android examples for User Interface:Layout

Description

layout View Auto

Demo Code


//package com.java2s;

import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;

public class Main {
    public static void layoutViewAuto(View view) {
        measureView(view);//from w ww . jav a  2s . c  om
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = view.getMeasuredWidth();
        lp.height = view.getMeasuredHeight();
    }

    /**
     * Measure a view.
     * @param child
     */
    public static void measureView(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
                    MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }
}

Related Tutorials