move View Center To - Android User Interface

Android examples for User Interface:View

Description

move View Center To

Demo Code


//package com.java2s;

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

public class Main {
    public static void moveViewCenterTo(View view, int x, int y) {//?xmllefttopmargin
        int width = view.getWidth();
        int height = view.getHeight();
        MarginLayoutParams params = (MarginLayoutParams) view
                .getLayoutParams();// w  ww.  j a  va2 s .  com
        params.leftMargin = x - width / 2;
        params.topMargin = y - height / 2;
        view.setLayoutParams(params);
    }
}

Related Tutorials