RelativeLayout动态设置边距、alignParent等属性

RelativeLayout有方法addRule可以动态设置alignParent、margin、控件相对位置的设置,比如需要设置childView右边距离RelativeLayout右边距为20dp:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View childView = findViewById(R.id.child1);
        RelativeLayout parent = findViewById(R.id.parent);
        RelativeLayout.LayoutParams lps = (RelativeLayout.LayoutParams) childView.getLayoutParams();
        lps.rightMargin = dip2px(this, 20.0f);
        lps.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

最终效果图:
《RelativeLayout动态设置边距、alignParent等属性》
上面的例子是相对于父布局的,再举一个相对于子控件的例子,实现下面的效果图:
《RelativeLayout动态设置边距、alignParent等属性》
红色childview2位于白色view左边20dp
代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View childView = findViewById(R.id.child1);
        RelativeLayout parent = findViewById(R.id.parent);
        RelativeLayout.LayoutParams lps = (RelativeLayout.LayoutParams) childView.getLayoutParams();
        lps.rightMargin = dip2px(this, 20.0f);
        lps.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        View childView2 = findViewById(R.id.child2);
        RelativeLayout.LayoutParams lps2 = (RelativeLayout.LayoutParams) childView2.getLayoutParams();
        //lps2.addRule(RelativeLayout.BELOW,R.id.child1);
        lps2.rightMargin = dip2px(this, 20.0f);
        lps2.addRule(RelativeLayout.LEFT_OF, R.id.child1);
    }

这时候addRule方法就要带一个view的id值。

有了这个addRule方法就可以设置很多其他属性了,通过源码可知RelativeLayout是使用mRules这个数组来记录哪种属性使用到了,一共有22种属性:

/**
     * Rule that aligns a child's right edge with another child's left edge.
     */
    public static final int LEFT_OF                  = 0;
    /**
     * Rule that aligns a child's left edge with another child's right edge.
     */
    public static final int RIGHT_OF                 = 1;
    /**
     * Rule that aligns a child's bottom edge with another child's top edge.
     */
    public static final int ABOVE                    = 2;
    /**
     * Rule that aligns a child's top edge with another child's bottom edge.
     */
    public static final int BELOW                    = 3;

    /**
     * Rule that aligns a child's baseline with another child's baseline.
     */
    public static final int ALIGN_BASELINE           = 4;
    /**
     * Rule that aligns a child's left edge with another child's left edge.
     */
    public static final int ALIGN_LEFT               = 5;
    /**
     * Rule that aligns a child's top edge with another child's top edge.
     */
    public static final int ALIGN_TOP                = 6;
    /**
     * Rule that aligns a child's right edge with another child's right edge.
     */
    public static final int ALIGN_RIGHT              = 7;
    /**
     * Rule that aligns a child's bottom edge with another child's bottom edge.
     */
    public static final int ALIGN_BOTTOM             = 8;

    /**
     * Rule that aligns the child's left edge with its RelativeLayout
     * parent's left edge.
     */
    public static final int ALIGN_PARENT_LEFT        = 9;
    /**
     * Rule that aligns the child's top edge with its RelativeLayout
     * parent's top edge.
     */
    public static final int ALIGN_PARENT_TOP         = 10;
    /**
     * Rule that aligns the child's right edge with its RelativeLayout
     * parent's right edge.
     */
    public static final int ALIGN_PARENT_RIGHT       = 11;
    /**
     * Rule that aligns the child's bottom edge with its RelativeLayout
     * parent's bottom edge.
     */
    public static final int ALIGN_PARENT_BOTTOM      = 12;

    /**
     * Rule that centers the child with respect to the bounds of its
     * RelativeLayout parent.
     */
    public static final int CENTER_IN_PARENT         = 13;
    /**
     * Rule that centers the child horizontally with respect to the
     * bounds of its RelativeLayout parent.
     */
    public static final int CENTER_HORIZONTAL        = 14;
    /**
     * Rule that centers the child vertically with respect to the
     * bounds of its RelativeLayout parent.
     */
    public static final int CENTER_VERTICAL          = 15;
    /**
     * Rule that aligns a child's end edge with another child's start edge.
     */
    public static final int START_OF                 = 16;
    /**
     * Rule that aligns a child's start edge with another child's end edge.
     */
    public static final int END_OF                   = 17;
    /**
     * Rule that aligns a child's start edge with another child's start edge.
     */
    public static final int ALIGN_START              = 18;
    /**
     * Rule that aligns a child's end edge with another child's end edge.
     */
    public static final int ALIGN_END                = 19;
    /**
     * Rule that aligns the child's start edge with its RelativeLayout
     * parent's start edge.
     */
    public static final int ALIGN_PARENT_START       = 20;
    /**
     * Rule that aligns the child's end edge with its RelativeLayout
     * parent's end edge.
     */
    public static final int ALIGN_PARENT_END         = 21;
    原文作者:whoami_I
    原文地址: https://blog.csdn.net/whoami_I/article/details/97135264
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞