您的位置:首页 > 其它

布局按钮Activity继承BaseActivity的使用

2013-04-25 19:50 375 查看
题记:写这篇博客要主是加深自己对布局按钮的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢。

信相在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布局:

<?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>


2.MainActivity:

每日一道理

曾经辉煌过,曾经凋零过,这可是你至死不渝的生活吗?我亲爱的母亲—大自然。多少次,我伏在地上,去聆听你沉重的脉搏声;多少次,我伫立在山前,去感受那松涛千年的浩瀚。你的豪壮,足以让中华民族腾飞;你的无私,谱写了一曲曲感人至深的千古壮曲。

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();
}
});
}
}

activity_main XML布局:

<?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>

3.IntentActivity:

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;
}
}
}

对应的button,textview布局:

<?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>

<?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>

文章结束给大家分享下程序员的一些笑话语录: 这个世界上只有10种人:懂得二进制的和不懂得二进制的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐