measure View via ViewGroup - Android User Interface

Android examples for User Interface:ViewGroup

Description

measure View via ViewGroup

Demo Code


//package com.java2s;

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

public class Main {
    public static void measureView(View view) {
        ViewGroup.LayoutParams p = view.getLayoutParams();

        if (p == null) {
            p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }//from w w  w . ja  v a  2 s  . co m
        int widthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
        int height = p.height;
        int heightSpec;
        if (height > 0) {
            heightSpec = MeasureSpec.makeMeasureSpec(height,
                    MeasureSpec.EXACTLY);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
        }
        view.measure(widthSpec, heightSpec);
    }
}

Related Tutorials