zoukankan      html  css  js  c++  java
  • android 使用代码实现 RelativeLayout布局

     1  RelativeLayout rl = new RelativeLayout(this);
     2         
     3         Button btn1 = new Button(this);
     4         btn1.setText("----------------------");
     5         btn1.setId(1);
     6         
     7         RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
     8         lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
     9         lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    10         // btn1 位于父 View 的顶部,在父 View 中水平居中
    11         rl.addView(btn1, lp1 );
    12        
    13         Button btn2 = new Button(this);
    14         btn2.setText("|\n|\n|\n|\n|\n|");
    15         btn2.setId(2);
    16        
    17         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    18         lp2.addRule(RelativeLayout.BELOW, 1);
    19         lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
    20         // btn2 位于 btn1 的下方、其左边和 btn1 的左边对齐
    21         rl.addView(btn2, lp2);
    22        
    23         Button btn3 = new Button(this);
    24         btn3.setText("|\n|\n|\n|\n|\n|");
    25         btn3.setId(3);
    26        
    27         RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    28        lp3.addRule(RelativeLayout.BELOW, 1);
    29         lp3.addRule(RelativeLayout.RIGHT_OF, 2);
    30         lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
    31         // btn3 位于 btn1 的下方、btn2 的右方且其右边和 btn1 的右边对齐(要扩充)
    32         rl.addView(btn3,lp3);
    33        
    34         Button btn4 = new Button(this);
    35         btn4.setText("--------------------------------------------");
    36         btn4.setId(4);
    37        
    38         RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    39         lp4.addRule(RelativeLayout.BELOW, 2);
    40         lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    41         // btn4 位于 btn2 的下方,在父 Veiw 中水平居中
    42         rl.addView(btn4,lp4);
    43        
    44        
    45         setContentView(rl);
  • 相关阅读:
    (十一)设置关闭多核cpu的核
    (十)修改开发板主频
    (九)How to use the audio gadget driver
    (8)全志A64查看寄存器
    内存溢出问题配置
    百度数据库优化经验
    如何让sql运行的更快
    百度性能优化
    spring ioc原理
    JNDI
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/2915337.html
Copyright © 2011-2022 走看看