Android examples for User Interface:Layout
position Relative Layout Child To Bottom Right Of View
//package com.java2s; import android.graphics.Rect; import android.view.View; import android.view.ViewGroup; public class Main { public static void positionRelativeLayoutChildToBottomRightOfView( View child, View view) { Rect viewLocation = getViewLocationRelativeTo(view, (View) child.getParent()); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child .getLayoutParams();//from w ww . j a va 2 s.co m params.leftMargin = viewLocation.right - child.getWidth(); params.topMargin = viewLocation.bottom; child.setLayoutParams(params); } public static Rect getViewLocationRelativeTo(View view, View relativeToView) { int[] viewLocation = new int[2]; view.getLocationInWindow(viewLocation); int[] relativeToViewLocation = new int[2]; relativeToView.getLocationInWindow(relativeToViewLocation); int x = viewLocation[0] - relativeToViewLocation[0]; int y = viewLocation[1] - relativeToViewLocation[1]; int w = view.getWidth(); int h = view.getHeight(); return new Rect(x, y, x + w, y + h); } }