天天看点

Android开发之FrameLayout布局

Android开发之FrameLayout布局

        在Android开发中,FrameLayout是所有布局容器中最简单的一种,在前边博客中有介绍关于Android开发中线性布局LinearLayout的应用。LinearLayout采用的是线性平铺的布局模式,FrameLayout也被称为帧布局。

        FrameLayout简单理解,可以将布局容器理解为一个单元素栈,先放入的视图在栈底,后放入的视图在栈顶,后放入的视图会覆盖先放入的视图。并且,FrameLayout不能够设置其内视图的位置,默认都是从左上角开始布局,这个布局模式在简单的重叠界面中使用十分方便。

        使用代码进行FrameLayout布局示例如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
//添加子视图
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new FrameLayout.LayoutParams(600,600));
textView1.setBackgroundColor(Color.RED);
frameLayout.addView(textView1);

TextView textView2 = new TextView(this);
textView2.setLayoutParams(new FrameLayout.LayoutParams(400,400));
textView2.setBackgroundColor(Color.YELLOW);
frameLayout.addView(textView2);

TextView textView3 = new TextView(this);
textView3.setLayoutParams(new FrameLayout.LayoutParams(200,200));
textView3.setBackgroundColor(Color.BLUE);
frameLayout.addView(textView3);

TextView textView4 = new TextView(this);
textView4.setLayoutParams(new FrameLayout.LayoutParams(100,100));
textView4.setBackgroundColor(Color.GREEN);
frameLayout.addView(textView4);
    }
      

继续阅读