RelativeLayout的addRule

RelativeLayout,顾名思义,就是以“相对”位置/对齐 为基础的布局方式。android.widget.RelativeLayout 有个 继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例调用 RelativeLayout.addView 就可以实现“相对布局”。
  android.widget.RelativeLayout.LayoutParams 有一个构造函数:RelativeLayout.LayoutParams(int w, int h),参数指定了子 View 的宽度和高度,这一点和其父类是一样的。而实现相对布局的关键在它的 两个 addRule 方法上。anchor 参数指定可以是 View 的 id(“相对于谁”)、RelativeLayout.TRUE(启用某种对齐方式) 或者 是-1(应用于某些不需要 anchor 的 verb);AddRule 方法的 verb 参数指定相对的“动作”(以下常量均定义于 android.widget.RelativeLayout中,为了简便不给出其全名):

  ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底边/左边/右边/顶边 和 anchor 指定的 View 的 底边/左边/右边/顶边 对齐。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、 ALIGN_WITH_PARENT_TOP : 和上面一组常量类似,只不过不需要再指定 anchor, 其 anchor 自动为 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 为 TRUE,在 Parent 中 水平居中/水平和垂直均居中/垂直居中。
RIGHT_OF、LEFT_OF、ABOVE、BELOW本View的位于指定View的右边、左边、上部、底部(POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT已经弃用)
示例:

public class AddRuleActivity extends Activity{
    private RelativeLayout relativeLayout;

    private Button b1;
    private Button b2;

    private Button b3;
    private Button b4;
    private Button b5;

    private static final int b1_id = 0x01;
    private static final int b2_id = 0x02;
    private static final int b3_id = 0x03;
    private static final int b4_id = 0x04;
    private static final int b5_id = 0x05;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        relativeLayout = new RelativeLayout(this);

        b1 = new Button(this);
        b1.setText("Button1");
        b1.setId(b1_id);
        RelativeLayout.LayoutParams p1 =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        p1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        p1.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
        relativeLayout.addView(b1,p1);


        b2 = new Button(this);
        b2.setText("Button2222222\n222");
        b2.setId(b2_id);
        RelativeLayout.LayoutParams p2 =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        //b2位于b1的下面而且是右对齐
        p2.addRule(RelativeLayout.ABOVE,b1_id);
        p2.addRule(RelativeLayout.ALIGN_RIGHT,b1_id);
        relativeLayout.addView(b2,p2);

        b3 = new Button(this);
        b3.setText("Button33");
        b3.setId(b3_id);
        RelativeLayout.LayoutParams p3 =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        //b3位于b2的右侧而且是底部对齐
        p3.addRule(RelativeLayout.ALIGN_BOTTOM,b2_id);
        p3.addRule(RelativeLayout.RIGHT_OF,b2_id);
        relativeLayout.addView(b3,p3);
        setContentView(relativeLayout);


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值