從零開始學android(FrameLayout幀布局.十四.)
FrameLayout布局(幀布局)就是在螢幕上開闢一個地區以填充所有的組件,但是使用FrameLayout布局會將所有的組件都放在螢幕的左上方,而且所有的組件可以層疊進行顯示。
<frameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </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 {@Overrideprotected 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