创建自己的基类activity

package com.example.baseactivitytest;

import android.support.v7.app.ActionBarActivity;

import android.annotation.SuppressLint;

import android.app.ActionBar;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

/*

 * 基类----使用系统提供的titleBar

 */

@SuppressLint("NewApi")

public class SzmActivity extends ActionBarActivity {


private OnBackClickListener backListenner;

private ActionBar mActionBar;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.mActionBar = getActionBar();

this.initActionBar(mActionBar);

}

public void initActionBar(ActionBar mActionBar) {

/*

* mActionBar.setDisplayUseLogoEnabled(true);

mActionBar.setLogo(R.drawable.ic_launcher);

mActionBar.setTitle("聊天");

mActionBar.setLogo(R.drawable.ic_launcher);

mActionBar.setDisplayHomeAsUpEnabled(true);

*/

}

public void setOnBackClickListener(OnBackClickListener backListenner){

this.backListenner=backListenner;

}

public interface OnBackClickListener {

public void OnBackClick();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == android.R.id.home) {

backListenner.OnBackClick();

return true;

}

return super.onOptionsItemSelected(item);

}

@Override

public void finish() {

super.finish();

overridePendingTransition(R.anim.back_enter_alpha,

R.anim.back_out_scale);

}

@Override

public void startActivity(Intent intent) {

super.startActivity(intent);

overridePendingTransition(R.anim.enter_scale, R.anim.out_alpha);

}

@Override

public void startActivityForResult(Intent intent, int requestCode) {

super.startActivityForResult(intent, requestCode);

overridePendingTransition(R.anim.enter_scale, R.anim.out_alpha);

}

}

使用方法:

package com.example.baseactivitytest;

import android.annotation.SuppressLint;

import android.app.ActionBar;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

@SuppressLint("NewApi")

public class SecondActivity extends SzmActivity {

Context context;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

context=this;

}

public void fff(View v){

Intent intent=new Intent(context,ThirdActivity.class);

startActivity(intent);

}

@Override

public void initActionBar(ActionBar mActionBar) {

super.initActionBar(mActionBar);

mActionBar.setTitle("聊天");

mActionBar.setLogo(R.drawable.ic_launcher);

mActionBar.setDisplayHomeAsUpEnabled(true);

this.setOnBackClickListener(new OnBackClickListener() {

@Override

public void OnBackClick() {

finish();

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.second, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}


如果想使用自己的布局,不想使用系统提供的titlebar,可以写一个新的基类:

package com.example.baseactivitytest;


import android.annotation.SuppressLint;

import android.app.Activity;

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.LinearLayout;

import android.widget.LinearLayout.LayoutParams;

import android.widget.TextView;

/*

 * 基类-----不使用系统提供的titleBar

 */

@SuppressLint("InflateParams")

public abstract class MyBaseActivity extends Activity {


public LayoutInflater mInflater;

private LinearLayout main_content,top_bar,bottom_bar;

private TextView tv_back,tv_content,tv_send;

private boolean isCustomTop,isCustomBottom;//是否自行导入了top布局---bottom布局

OnMyOnClickListenner myClickner;

public static final int BACK=1;//回调自定义常量

public static final int CONTENT=2;

public static final int SURE=3;

@Override

protected void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);

super.onCreate(savedInstanceState);

mInflater = LayoutInflater.from(this);

setContentView(R.layout.activity_mybase);

top_bar=(LinearLayout) findViewById(R.id.top_bar);

main_content=(LinearLayout) findViewById(R.id.mian_content);

bottom_bar=(LinearLayout) findViewById(R.id.bottom_bar);

}

/*

* 自己写方法给子类使用,用来初始化界面

*/

public void setMyContentView(int layoutId){

View v=mInflater.inflate(layoutId, main_content,false);

main_content.removeAllViews();

main_content.addView(v,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

View topV=mInflater.inflate(R.layout.in_list_tt, null);

tv_back=(TextView) topV.findViewById(R.id.tv_back);

tv_content=(TextView) topV.findViewById(R.id.tv_content);

tv_send=(TextView) topV.findViewById(R.id.tv_send);

tv_send.setOnClickListener(new MyOnClickListenner());

tv_back.setOnClickListener(new MyOnClickListenner());

setTopBar(topV);

isCustomTop=false;

View bottomV=mInflater.inflate(R.layout.in_list_tt, null);

setBottomBar(bottomV);

}

public void setOnMyOnClickListenner(OnMyOnClickListenner clickner){

this.myClickner=clickner;

}

/*

* 设置top布局详情

*/

public void setTopBar(View v){

isCustomTop=true;

top_bar.removeAllViews();

top_bar.addView(v,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80));

}

/*

* 设置bottom布局详情

*/

public void setBottomBar(View v){

bottom_bar.removeAllViews();

bottom_bar.addView(v,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80));

}

/*

* 设置是否显示top布局

*/

public void setTopLayVisable(int n){

top_bar.setVisibility(n);

}

/*

* 设置是否显示bottom布局

*/

public void setBottomLayVisable(int n){

bottom_bar.setVisibility(n);

}

public class MyOnClickListenner implements OnClickListener{


@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.tv_back:

myClickner.myClick(BACK);

break;

case R.id.tv_send://最右侧的按钮回调

myClickner.myClick(SURE);

break;

default:

break;

}

}

}

/*

* 点击回调接口

*/

public interface OnMyOnClickListenner{

public void myClick(int a);

};

@Override

public void finish() {

super.finish();

overridePendingTransition(R.anim.back_enter_alpha,

R.anim.back_out_scale);

}

@Override

public void startActivity(Intent intent) {

super.startActivity(intent);

overridePendingTransition(R.anim.enter_scale, R.anim.out_alpha);

}

@Override

public void startActivityForResult(Intent intent, int requestCode) {

super.startActivityForResult(intent, requestCode);

overridePendingTransition(R.anim.enter_scale, R.anim.out_alpha);

}

}

下面是需要的布局文件:

R.layout.activity_mybase:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" 

    android:orientation="vertical">

    

    <LinearLayout 

        android:layout_width="fill_parent"

        android:layout_height="50dp"

        android:id="@+id/top_bar"

        android:background="#00f"

        android:gravity="center_vertical"

        android:orientation="horizontal"

        >

        

    </LinearLayout>

    

<LinearLayout 

   android:id="@+id/mian_content"

   android:layout_width="fill_parent"

   android:layout_height="0dp"

   android:layout_weight="1"

   android:orientation="vertical"

   >

   

</LinearLayout>

    

<LinearLayout 

   android:layout_width="fill_parent"

   android:layout_height="50dp"

   android:id="@+id/bottom_bar"

   android:gravity="center_vertical"

   android:background="#0f0"

   android:orientation="horizontal"

   >

   

</LinearLayout>

</LinearLayout>

R.layout.in_list_tt:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal"

    android:gravity="center_vertical"

>

    

    <TextView 

        android:id="@+id/tv_back"

        android:layout_width="60dp"

        android:layout_height="wrap_content"

        android:text="返回"

        android:gravity="center_vertical|right"

        android:textSize="17sp"

        android:layout_marginTop="10dp"

        android:drawableLeft="@drawable/ic_launcher"

        android:layout_marginBottom="10dp"

        android:layout_marginLeft="5dp"

        />


    <TextView 

        android:id="@+id/tv_content"

        android:layout_width="0dp"

        android:layout_weight="1"

        android:gravity="center"

        android:textSize="17sp"

        android:layout_height="wrap_content"

        android:layout_marginTop="5dp"

        android:layout_marginBottom="5dp"

        android:text="233"

        />

    

    <TextView 

        android:id="@+id/tv_send"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_vertical"

        android:layout_marginRight="8dp"

         android:textSize="17sp"

        android:text="确定"

        />

    


</LinearLayout>


使用方法参照:

使用基类头布局:

package com.example.baseactivitytest;


import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;


public class ThirdActivity extends MyBaseActivity {


Context context;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setMyContentView(R.layout.activity_third);

context=this;

this.setOnMyOnClickListenner(new OnMyOnClickListenner() {

@Override

public void myClick(int a) {

switch (a) {

case SURE:

Toast.makeText(context, "sddd", 0).show();

Intent intent=new Intent(context,FourActivity.class);

startActivity(intent);

break;

case BACK:

Toast.makeText(context, "xxxx", 0).show();

ThirdActivity.this.finish();

break;

default:

break;

}

// Toast.makeText(context, "sddd", 0).show();

}

});

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.third, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

自己 导入头布局:

package com.example.baseactivitytest;


import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


public class FourActivity extends MyBaseActivity {


EditText et_search;

TextView textView1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setMyContentView(R.layout.activity_four);

textView1=(TextView) findViewById(R.id.textView1);

View v=mInflater.inflate(R.layout.item_topbar2, null);

Button btn_search=(Button) v.findViewById(R.id.btn_search);

btn_search.setOnClickListener(new FourOnClickListenner());

et_search=(EditText) v.findViewById(R.id.ed_search);

setTopBar(v);

}


public class FourOnClickListenner implements OnClickListener{


@Override

public void onClick(View v) {

textView1.setText(et_search.getText().toString().trim());

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.four, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}



你可能感兴趣的:(创建自己的基类activity)