FrameLayout布局(帧布局)就是在屏幕上开辟一个区域以填充所有的组件,但是使用FrameLayout布局会将所有的组件都放在屏幕的左上角,而且所有的组件可以层叠进行显示。
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="322dp"
android:src="@drawable/KILL" />
android:id="@+id/button1"
android:layout_width="240dp"
android:layout_height="107dp"
android:text="Button" />
android:id="@+id/textView1"
android:layout_width="154dp"
android:layout_height="62dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
…………………………………………………………毫无美感的分割线…………………………………………………………
不需要xml文件,直接在JAVA文件中配置项目
package com.example.framelayout;
import android.os.Bundle;
import android.app.Activity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout layout = new FrameLayout(this);
// 为布局设置宽度和高度
FrameLayout.LayoutParams LayoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
// 为图片设置高度和宽度
FrameLayout.LayoutParams imageLayoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 311);
// 为按钮设置宽度和高度
FrameLayout.LayoutParams buttonLayoutParams = new FrameLayout.LayoutParams(
281, 173);
// 为文字设置宽和高
FrameLayout.LayoutParams textLayoutParams = new FrameLayout.LayoutParams(
183, 85);
ImageView imageView = new ImageView(this);// 创建ImageView对象
imageView.setImageResource(R.drawable.kill);// 设置图片信息
layout.addView(imageView, imageLayoutParams);// 将imageView添加到Framelayout布局当中
Button button = new Button(this);//创建Button对象
button.setText("button");//设置标题
layout.addView(button, buttonLayoutParams);//将按钮增加到Framelayout布局当中
TextView textView=new TextView(this);//创建textView对象
textView.setText("TextView");//设置标题
layout.addView(textView, textLayoutParams);//将TextView添加到Framelayout当中
super.addContentView(layout,LayoutParams);//将framelayout添加到content中
}
}
大家可以看到使用JAVA文件配置和使用xml文件配置的效果是相同的,XML布局的方式和动态布局大家可以根据自己的需要自行设定
下节预报:表格布局Tablelayout
原文:http://blog.csdn.net/u013616976/article/details/38535485