0

当我尝试使用此代码设置边距时:

 @Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
RelativeLayout.LayoutParams params;
TextView login = (TextView) findViewById(R.id.textView2);
int text_height = login.getHeight();
int text_whidth= login.getWidth();
params = new RelativeLayout.LayoutParams((int)login.getLayoutParams().WRAP_CONTENT, (int)login.getLayoutParams().WRAP_CONTENT);
        params.setMargins(0, 500, 0, 0);

login.setLayoutParams(params);

}

该应用程序在启动时崩溃。如何设置保证金而不会使我的应用程序崩溃?

谢谢你的帮助。

4

3 回答 3

2

尝试替换以下行:

params = new RelativeLayout.LayoutParams((int)login.getLayoutParams().WRAP_CONTENT, (int)login.getLayoutParams().WRAP_CONTENT);

有了这个:

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
于 2013-09-08T14:02:20.733 回答
1

这是你的方法,只需要传递参数

public static void setMargin(View view, int left, int right, int top, int bottom) {
    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) 
    view.getLayoutParams();
    params.setMargins(left, top, right, bottom);
    view.setLayoutParams(params);
}

其中第一个参数视图是您的 TextView。

于 2018-12-28T04:46:24.830 回答
0

LayoutParams 具有setMargins方法,然后将参数设置setLayoutParams()TextView.

LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10); // setMargins(left, top, right, bottom)
textView.setLayoutParams(params);
于 2018-12-28T04:11:20.393 回答