FrameLayout布局(幀布局)就是在螢幕上開辟一個區域以填充所有的元件,但是使用FrameLayout布局會将所有的元件都放在螢幕的左上角,而且所有的元件可以層疊進行顯示。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="322dp"
android:src="@drawable/KILL" />
<Button
android:id="@+id/button1"
android:layout_width="240dp"
android:layout_height="107dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="154dp"
android:layout_height="62dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
…………………………………………………………毫無美感的分割線…………………………………………………………
不需要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