android的布局管理器

理论上通过setContentView(view)能够把一个view设置到activity中,但当你有很多个view控件的时候,就需要用android的布局管理器来管理view控件了。

android布局管理器有以下几种:

1.线性布局  LinearLayout

2.框架布局  FrameLayout

3.表格布局  TableLayout

4.相对布局  RelativeLayout

5.绝对布局  AbsoluteLayout

 

一、LinearLayout 线性布局管理器

线性布局分为水平布局和垂直布局两种。水平布局就是把view水平排列,通过layout.setOrientation(LinearLayout.HORIZONTAL);垂直是吧View垂直排列,通过layout.setOrientation(LinearLayout.VERTICAL)来实现。

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        LinearLayout lly = new LinearLayout(this);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        lly.setOrientation(LinearLayout.VERTICAL);
        
        setContentView(lly, llp);
        
        LinearLayout.LayoutParams viewparams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT);
        
        TextView tv1 = new TextView(this);
        tv1.setText("This is Text1");
        tv1.setBackgroundColor(Color.rgb(0, 255, 30));
        
        TextView tv2 = new TextView(this);
        tv2.setText("This is Text2");
        tv2.setBackgroundColor(Color.rgb(255, 30, 60));
        
        lly.addView(tv1,viewparams);
        lly.addView(tv2,viewparams);
        
    }    

}

上边代码中Layoutparams llp是为了保证setContentView时候 lly布局能够填满整个屏幕;而第二个Layoutparams viewparams则是为了让lly里边的控件都能够保持长度填满lly,高度取控件自身高度,最后通过addView(view, params),通过实验发现其实这里不设置params,直接addView(view)的效果是一样的,都是长填满,高取控件高度。

 

二、FrameLayout 框架布局管理器

这个感觉没有什么太大的作用,FrameLayout就是把布局里边的所有控件都放到左上角,并逐个覆盖。

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        FrameLayout ll = new FrameLayout(this);
        FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        
        setContentView(ll, llp);
        
        FrameLayout.LayoutParams viewparams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT);
        
        TextView tv1 = new TextView(this);
        tv1.setText("This is Text1Text1Text1.");
        tv1.setBackgroundColor(Color.rgb(0, 255, 30));
        
        TextView tv2 = new TextView(this);
        tv2.setText("This is Text2");
        tv2.setBackgroundColor(Color.rgb(255, 30, 60));
        
        ll.addView(tv1,viewparams);
        ll.addView(tv2,viewparams);        
    }
}

 

三、TableLayout 表格布局管理器

 

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        TableLayout tl = new TableLayout(this);
        TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT);
        tl.setBackgroundColor(Color.GREEN);
        setContentView(tl, tlp);
        
        String[][] data = new String[][]{
                {"姓名", "学号", "年级", "职务", "住址"},
                {"小明", "1000", "3", "班长", "北京市西山区第一街365号"},
                {"小兰", "2001", "6", "班长,学习委员", "北京市海淀区春熙路63号"}
        };
        
        for(int i=0; i<data.length; i++)
        {
            TableRow row = new TableRow(this);
            for(int j=0; j<data[i].length; j++)
            {
                TextView tv = new TextView(this);
                tv.setText(data[i][j]);
                row.addView(tv);
            }
            tl.addView(row);
        }
        
        //tl.setShrinkAllColumns(true);    //所有列都可折叠
        //tl.setColumnShrinkable(3, true);  //第4列可折叠
        //tl.setColumnCollapsed(2, true);   //第3列不显示
    }
}
正常:


折叠:


不显示:



四、RelativeLayout 相对布局管理器

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        RelativeLayout rl = new RelativeLayout(this);
        RelativeLayout.LayoutParams rlp0 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rl.setBackgroundColor(Color.rgb(102, 255, 179));
        setContentView(rl, rlp0);
        
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, 1);
        rlp.leftMargin = 10;
        rlp.topMargin = 10;
        
        TextView tv1 = new TextView(this);
        tv1.setText("This is Text1");
        tv1.setBackgroundColor(Color.GREEN);
        tv1.setId(1);
        rl.addView(tv1);
        
        TextView tv2 = new TextView(this);
        tv2.setText("This is Text2");
        tv2.setBackgroundColor(Color.BLUE);
        rl.addView(tv2, rlp);
        
        
        RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rlp2.addRule(RelativeLayout.RIGHT_OF, 1);
        rlp2.leftMargin = 10;
        
        
        TextView tv3 = new TextView(this);
        tv3.setText("This is Text3");
        tv3.setBackgroundColor(Color.YELLOW);
        rl.addView(tv3, rlp2);
        
    }
}

本来想用一个layoutParams通过addRule和removeRule的方式来控制所有的RelativeLayout中的view控件,但实验中却发现要使用removeRule必须把androidSDK改到17(本来是8),还有就是,layoutParams会把所有对它进行的设置都更改之后再去设置控件,有点不懂哈,上例子:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, 1);
        
        TextView tv1 = new TextView(this);
        tv1.setText("This is Text1");
        tv1.setBackgroundColor(Color.GREEN);
        tv1.setId(1);
        rl.addView(tv1);    

      TextView tv2 = new TextView(this);
      tv2.setText("This is Text2");
      tv2.setBackgroundColor(Color.GRAY);
      rl.addView(tv2, rlp);

        rlp.addRule(RelativeLayout.RIGHT_OF, 1);

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, 1);
        
        rlp.addRule(RelativeLayout.RIGHT_OF, 1);
        
        TextView tv1 = new TextView(this);
        tv1.setText("This is Text1");
        tv1.setBackgroundColor(Color.GREEN);
        tv1.setId(1);
        rl.addView(tv1);

     TextView tv2 = new TextView(this);
     tv2.setText("This is Text2");
     tv2.setBackgroundColor(Color.GRAY);
     rl.addView(tv2, rlp);

结果是一样的,也就是说不管 rlp2.addRule(RelativeLayout.RIGHT_OF, 1);位置在rl.addView(tv2, rlp)的前边或者后边,结果都一样,这点和传统上的顺序执行有点不一样。




 

posted on 2014-08-15 16:36  atomgame的记事本  阅读(407)  评论(0编辑  收藏  举报

导航