SetMargin在RelativeLayout中不起作用

时间:2014-09-14 18:33:31

标签: android relativelayout margin

drawer.xml;具有RelativeLayout id:relative。

我想在drawerlayout上显示另一个相对布局;

RelativeLayout drawer = (RelativeLayout) getApplicationContext().findViewById(R.id.drawerrelative);

RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(R.id.relative, (ViewGroup) findViewById(R.id.relative));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_IN_PARENT);
drawer.addView(view, params);

我在主要布局上显示相对布局(id:relative)(id:drawerrelative)和屏幕上的CENTER。正常情况下,但是当我设置边距时;

params.setMargins(0, 100, 0, 0);

不工作。我添加了view.requestLayout()但没有工作..

我如何解决这个问题? (谢谢你和我的英语不好的故事)

1 个答案:

答案 0 :(得分:0)

试试此代码块

LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(0, 100, 0, 0);
relativeLayout.setLayoutParams(view);
relativeLayout.requestLayout();