Activity繼承BaseActivity的使用(使用相同佈局)

嘟嘟嚕嘟嘟發表於2014-07-30

相信在android應用開發中有不少的相同佈局,這時候當然你可以對於每個Activity去設定不同的xml,可是有相同的xml佈局怎麼辦呢,再寫一遍其不是很浪費時間,這時候我們可以寫一個基類用來處理相同的佈區域性分!詳見如下:





1.BaseActivity程式碼部分:

package com.example.com.test.test1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BaseActivity extends Activity {

//	共用的宣告部分
	private TextView head_tv;
	private ImageButton imageButton_exit;
//	宣告分配佈局的layoutID,如果分配的是RelativeLayout,也可以,根據自己需求來換
	private LinearLayout llcontent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
//		去掉系統的TitleBar
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.base);
	}
//	初始化
	public void BaseInit(){
		head_tv =(TextView)findViewById(R.id.header_tv);
		imageButton_exit = (ImageButton)findViewById(R.id.imageButton_exit);
	}
//  設定要顯示的佈局方法
	public void BaseSetContentView(int layoutID){
		llcontent = (LinearLayout)findViewById(R.id.llcontent);
//		獲得inflater
		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		把繼承該BaseAcitivyt的layoutID放進來 顯示
		View view = inflater.inflate(layoutID, null);
//		addview
		llcontent.addView(view);
	}
//  設定要下一個顯示的佈局
	public void BaseSetContentView_other(int layoutID){
		llcontent = (LinearLayout)findViewById(R.id.llcontent_other);
//		獲得inflater
		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		把繼承該BaseAcitivyt的layoutID放進來 顯示
		View view = inflater.inflate(layoutID, null);
//		addview
		llcontent.addView(view);
	}
//  獲得head_tv
	public TextView getHead_tv(){
		return head_tv;
	}
//	設定TitleBar的textview-----
	public void setHead_tv(String text){
		head_tv.setText(text);
	}
//	獲得exit按鈕
	public ImageButton getExit(){
		return imageButton_exit;
	}
//	返回上一個Activity的方法
	public void backIntent(final Context context,final Class<?> toClass){
		imageButton_exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, toClass);
				startActivity(intent);
				finish();
			}
		});
	}

}

對應的base XML佈局:

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="horizontal" 
>

    <RelativeLayout
        android:id="@+id/frameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/top_bar_bg"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

     

        <ImageButton
            android:id="@+id/imageButton_exit"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:background="@drawable/closed_press" 
          />

        <TextView
            android:id="@+id/header_tv"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_marginRight="45dp"
            android:layout_toRightOf="@+id/imageButton_mainMenu"
            android:gravity="center"
            android:textColor="#008AE0"
            android:textSize="15dp" 
            android:text="共用的TitleBar"/>

        <View
            android:id="@+id/view1"
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageButton_exit"
            android:background="#000000" />
        </RelativeLayout>
        <!-- 為下個佈局分配空間 -->
        <LinearLayout 
            android:id="@+id/llcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal"
            >
        </LinearLayout>
        <!-- 為下個不同分佈的佈局分配空間,可以滿足你不同的佈局需求 -->
        <LinearLayout 
            android:id="@+id/llcontent_other"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            ></LinearLayout>
        </RelativeLayout></pre><br>
<pre></pre>
<p>2.MainActivity:</p>
<p></p>
<pre name="code" class="java">package com.example.com.test.test1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends BaseActivity {

//	宣告activity_main XML中的控制元件
	private Button firstbtn,secondbtn;
	private final int FIRST = 0;
	private final int SECOND = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
//      呼叫父類方法顯示view
        BaseSetContentView(R.layout.activity_main);
        LocalInit();
    }
//  初始化
    private void LocalInit(){
    	BaseInit();
    	firstbtn = (Button)findViewById(R.id.first_btn);
    	secondbtn = (Button)findViewById(R.id.second_btn);
//    	呼叫基類的sethead_tv和exit()
    	setHead_tv("這是MainActivity!");
//    	獲得基類按鈕並監聽
        getExit().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
				dialog.setTitle("提示:");
				dialog.setMessage("退出程式?");
				dialog.setCancelable(false);
				dialog.setPositiveButton("Y", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
						System.exit(0);
					}
				}).setNegativeButton("N", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
			}
		});
    	
    	firstbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, IntentActivity.class);
				intent.putExtra("option",FIRST);
				startActivity(intent);
				finish();
			}
		});
    	
    	secondbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, IntentActivity.class);
				intent.putExtra("option",SECOND);
				startActivity(intent);
				finish();
			}
		});
    }
}
</pre>activity_main XML佈局:
<p></p>
<p></p>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    
<Button android:id="@+id/first_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="跳轉ButtonActivity"/>
<Button android:id="@+id/second_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:text="跳轉TextViewActivity"/>
</LinearLayout></pre><br>
3.IntentActivity:
<p></p>
<p></p>
<pre name="code" class="java">package com.example.com.test.test1;

import android.os.Bundle;

public class IntentActivity extends BaseActivity {
//  接收MainActivity的傳值
	private int option;
	private final int FIRST = 0;
	private final int SECOND = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity();
	}
	
// 初始化
	private void LocalInit(){
		BaseInit();
//		呼叫BaseActivity中exit按鈕返回的方法
		backIntent(IntentActivity.this, MainActivity.class);
	}
	private void getActivity(){
//		接收傳值
		Bundle bundle = getIntent().getExtras();
		option = bundle.getInt("option");
		switch(option){
		case FIRST:
		
//			顯示一種佈局
			BaseSetContentView_other(R.layout.button);
			LocalInit();
			setHead_tv("顯示ButtonActivity!");
			break;
		case SECOND:
			
//			顯示令一種佈局
			BaseSetContentView_other(R.layout.textview);
			LocalInit();
			setHead_tv("顯示TextViewActivity!");
			break;
	}
	}
}
</pre><br>
對應的button,textview佈局:
<p></p>
<p></p>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
   >
    
<Button android:id="@+id/first_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="我是按鈕Acitivity"/>
<Button android:id="@+id/second_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="返回請點選TitleBar返回按鈕"/>
<Button android:id="@+id/three_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="我是按鈕Acitivity"/>
<Button android:id="@+id/four_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="返回請點選TitleBar返回按鈕"/>
</LinearLayout></pre><br>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
<TextView android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextViewActivity,返回請點選TitleBar返回按鈕!同時對比下各Activity的佈局情況"
    android:textSize="15sp"
    android:textColor="#008ae0"/>

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="注意體會佈局"/></LinearLayout>
</pre>
<pre></pre>
<pre></pre>


相關文章