//方法一:经过布局设置在父控件的位置。可是必需要有父控件, 并且要指定父布局的类型,很差的方法。 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLeft() + offsetX; layoutParams.topMargin = getTop() + offsetY; setLayoutParams(layoutParams); /**=============================================== * 方法二:用ViewGroup的MarginLayoutParams的方法去设置marign * 优势:相比于上面方法, 就不须要知道父布局的类型。 * 缺点:滑动到右侧控件会缩小 *===============================================*/ ViewGroup.MarginLayoutParams mlayoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams(); mlayoutParams.leftMargin = getLeft() + offsetX; mlayoutParams.topMargin = getTop() + offsetY; setLayoutParams(mlayoutParams);
MarginLayoutParams params = (MarginLayoutParams) mButton.getLayoutParams(); params.width += 10; params.height += 10; mButton.setLayoutParams(params);