set View Margin Top - Android User Interface

Android examples for User Interface:View Margin

Description

set View Margin Top

Demo Code


//package com.java2s;

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

public class Main {

    public static void setMarginTop(View view, int top) {
        setMargins(view, 0, top, 0, 0);//from   w  w  w.j ava  2s .c om
    }

    public static void setMargins(View view, int left, int top, int right,
            int bottom) {
        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view
                    .getLayoutParams();
            p.setMargins(left, top, right, bottom);
            view.requestLayout();
        }
    }
}

Related Tutorials