從零開始學android(FrameLayout幀布局.十四.)

來源:互聯網
上載者:User

從零開始學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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.