• android 使用代码实现 RelativeLayout布局


     

    参照以下博客和代码:

    1 http://blog.csdn.net/gf771115/article/details/8237229

    2 http://blog.csdn.net/love_world_/article/details/8426935

    RelativeLayout rl = new RelativeLayout(this);

    Button btn1 = new Button(this);
    btn1.setText("----------------------");
    btn1.setId(1);

    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    // btn1 位于父 View 的顶部,在父 View 中水平居中
    rl.addView(btn1, lp1 );

    Button btn2 = new Button(this);
    btn2.setText("| | | | | |");
    btn2.setId(2);

    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp2.addRule(RelativeLayout.BELOW, 1);
    lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
    // btn2 位于 btn1 的下方、其左边和 btn1 的左边对齐
    rl.addView(btn2, lp2);

    Button btn3 = new Button(this);
    btn3.setText("| | | | | |");
    btn3.setId(3);

    RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp3.addRule(RelativeLayout.BELOW, 1);
    lp3.addRule(RelativeLayout.RIGHT_OF, 2);
    lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
    // btn3 位于 btn1 的下方、btn2 的右方且其右边和 btn1 的右边对齐(要扩充)
    rl.addView(btn3,lp3);

    Button btn4 = new Button(this);
    btn4.setText("--------------------------------------------");
    btn4.setId(4);

    RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp4.addRule(RelativeLayout.BELOW, 2);
    lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    // btn4 位于 btn2 的下方,在父 Veiw 中水平居中
    rl.addView(btn4,lp4);


    setContentView(rl);

  • 相关阅读:
    linux 添加secondary ip
    最大传输单元(MTU)
    【PSY】 [歌詞] 父親
    make clean与make distclean的区别
    flex就是b/s中的c/s,要装插件flash player
    as中的replace只写了一个参数
    6个简单的解决方案解决Internet Explorer中的透明度问题
    9个优秀网上免费标签云生成工具
    2012年网页设计趋势
    11个非常漂亮动物为主题的高品质图标集
  • 原文地址:https://www.cnblogs.com/exmyth/p/4634380.html
Copyright © 2020-2023  润新知