動態加入 – 加入其他的layout
July 28, 2013 ·
0 Comments
動態加入 – 加入其他的layout
SDK版本: API level 1 ,Android 1.0
在這裡介紹如何在程式之中,即時動態的加入畫面檔案XML檔。一開始的時候我們需要設置指定兩個XML檔案,然後我們透過程式的方法把它動態的加入。
這功能還蠻好用的,這個效果能可以讓你的應用程式看起來很華麗,運用起來也相當的方便。
其中activity_main.xml XML碼如下:
範例 Ch6-8 activity_main.xml (範例程式中sample\ch6\Tutorial_layout_add\res\layout\activity_main.xml
- <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
- xmlns:tools=”http://schemas.android.com/tools”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent”
- android:paddingBottom=”@dimen/activity_vertical_margin”
- android:paddingLeft=”@dimen/activity_horizontal_margin”
- android:paddingRight=”@dimen/activity_horizontal_margin”
- android:paddingTop=”@dimen/activity_vertical_margin”
- tools:context=”.MainActivity” >
- <TextView
- android:id=”@+id/textView1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”@string/hello_world” />
- <LinearLayout
- android:id=”@+id/linearLayout1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignRight=”@+id/textView1″
- android:layout_below=”@+id/textView1″
- android:orientation=”vertical” >
- </LinearLayout>
- <Button
- android:id=”@+id/button1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignParentRight=”true”
- android:layout_alignParentTop=”true”
- android:layout_marginRight=”18dp”
- android:text=”Button” />
- </RelativeLayout>
XML解說:
- 第17-23行: 在這裡呢特別準備一個LinearLayout,然後到時候程式執行時,別會把另外一個畫面的放到這裡。
- 第26-33行: 在這裡我們加上一個按鈕,可以讓用戶按下按鈕之後,透過程式動態的加入另外一個畫面。
XML的表現:
圖 6-8-1 activity_main.xml 使用所定義出來的效果。
範例 Ch6-6 page2.xml (範例程式中sample\ch6\Tutorial_layout_add\res\layout\page2.xml )
- <?xml version=”1.0″ encoding=”utf-8″?>
- <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
- android:layout_width=”match_parent”
- android:layout_height=”match_parent” >
- <TextView
- android:id=”@+id/textView1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignParentLeft=”true”
- android:layout_alignParentTop=”true”
- android:text=”new layout” />
- <Button
- android:id=”@+id/button1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_alignParentLeft=”true”
- android:layout_below=”@+id/textView1″
- android:text=”Button” />
- </RelativeLayout>
那在page2 .xml我們只是隨便放了幾個元件,並沒有特別的設計。
XML的表現:
圖 6-8-2 page2.xml 使用所定義出來的效果。
- package com.powenko.tutorial_layout_add;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button1=(Button)findViewById(R.id.button1);
- button1.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- LinearLayout linearLayout1=(LinearLayout)findViewById(R.id.linearLayout1);
- LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.page2 , null, true); //讀取的page2.
- linearLayout1.addView(view); //加入畫面上
- }});
- }
- }
程式說明:
這一段程式的意思是
- 第24行: 取得 LinearLayout的ID。
- 第25行: 取得系統的 LayoutInflater。
- 第26行: 讀取 page2的資料後放入 View之中。
- 第27行: 加入畫面上。
執行結果:
範例程式的執行結果。
圖 6-8-3 範例結果。
執行影片:
範例執行影片 6-8-addLayout.mov(範例光碟中video\ch6\6-8-addLayout.mov) 或 http://youtu.be/DaoTbDmeYWY
By admin-powenko