//方法一:經過佈局設置在父控件的位置。可是必需要有父控件, 並且要指定父佈局的類型,很差的方法。 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);