【问题标题】:SetMargin Not Working in RelativeLayoutSetMargin 在相对布局中不起作用
【发布时间】:2014-11-08 07:22:23
【问题描述】:

抽屉.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:drawerrelative)和屏幕中心显示相对布局(id:relative)。一切正常,但当我设置边距时;

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

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

我该如何解决这个问题? (谢谢你,对不起我的英语不好)

【问题讨论】:

  • 请解释一下“不工作”对您意味着什么。
  • 我在中心屏幕上显示布局,但我想设置边距顶部,因为我想显示布局中心屏幕和底部附近的几个像素

标签: android android-relativelayout margin


【解决方案1】:

试试这个代码块

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();

【讨论】:

    猜你喜欢
    • 2013-01-19
    • 2011-12-31
    • 2018-05-12
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多