create Child View Height Measure Spec - Android User Interface

Android examples for User Interface:View Child

Description

create Child View Height Measure Spec

Demo Code


//package com.java2s;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout.LayoutParams;

public class Main {

    public static int createChildHeightMeasureSpec(
            int parentHeightMeasureSpec, View view) {
        int parentHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec);
        int parentHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec);

        int childHeightMeasureSpec = 0;

        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();

        if (parentHeightMode == MeasureSpec.EXACTLY) {
            if (layoutParams.height > 0) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        layoutParams.height, MeasureSpec.EXACTLY);
            } else if (layoutParams.height == LayoutParams.WRAP_CONTENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        parentHeightSize, MeasureSpec.AT_MOST);
            } else if (layoutParams.height == LayoutParams.MATCH_PARENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        parentHeightSize, MeasureSpec.EXACTLY);
            }//from  w w  w  . jav a2s  .co m
        } else if (parentHeightMode == MeasureSpec.AT_MOST) {
            if (layoutParams.height > 0) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        layoutParams.height, MeasureSpec.EXACTLY);
            } else if (layoutParams.height == LayoutParams.WRAP_CONTENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        parentHeightSize, MeasureSpec.AT_MOST);
            } else if (layoutParams.height == LayoutParams.MATCH_PARENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        parentHeightSize, MeasureSpec.EXACTLY);
            }
        } else if (parentHeightMode == MeasureSpec.UNSPECIFIED) {
            if (layoutParams.height > 0) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        layoutParams.height, MeasureSpec.EXACTLY);
            } else if (layoutParams.height == LayoutParams.WRAP_CONTENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
                        MeasureSpec.UNSPECIFIED);
            } else if (layoutParams.height == LayoutParams.MATCH_PARENT) {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
                        MeasureSpec.UNSPECIFIED);
            }
        }

        return childHeightMeasureSpec;
    }
}

Related Tutorials