set View Layout Params - Android User Interface

Android examples for User Interface:Layout

Description

set View Layout Params

Demo Code


//package com.java2s;

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

import android.widget.FrameLayout;
import android.widget.LinearLayout;

import android.widget.RelativeLayout;

public class Main {
    public static void setViewLayoutParam(View view,
            ViewGroup.LayoutParams params) {
        // fill_parent -1, match_parent -1, wrap_content -2.
        ViewGroup.LayoutParams target;//  w  w  w .j a v a  2  s. c o m
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent == null) {
            target = new FrameLayout.LayoutParams(params);
        } else if (parent instanceof FrameLayout) {
            target = new FrameLayout.LayoutParams(params);
        } else if (parent instanceof LinearLayout) {
            target = new LinearLayout.LayoutParams(params);
        } else if (parent instanceof RelativeLayout) {
            target = new RelativeLayout.LayoutParams(params);
        } else {
            // Note that there are some other LayoutParams, e.g. AbsListView.LayoutParams.
            // but we only handle layout here.
            target = params;
        }
        // view LayoutParams class type depends on view's parent layout.
        view.setLayoutParams(target);
    }
}

Related Tutorials