Reusing Action Bar in all activities of applicatio

2019-05-14 01:34发布

问题:

I have created a MenuActivity which is having an Action Bar and an Split Action Bar. I want to use this actionbar and splitactionbar view for all activities in my application. I am a newbie to android so can somebody guide me stepwise about this.

Also I am trying to put the search icon on actionbar which is right now appearing on SplitActionBar. I have four icons on SplitActionBar and i want to show search icon on the actionbar not on the SplitActionBar. The search icon is a SearchView item which when clicked expands on ActionBar, which is very untidy. I want it to appear on rightmost position in the ActionBar and expand on the same when clicked.

This is MenuACtivity.java:

package com.example.travelplanner;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.travelplanner.R;

public class MenuActivity extends Activity implements OnItemClickListener{

    Timer t1;
    TimerTask tt1;
    ImageView slide;
    int currindex = 0;
    ActionBar actionBar;
    ArrayList<ItemDetails> image_details = GetSearchResults();
    private int IMAGE_IDS[] = {R.drawable.slide1, R.drawable.slide2, R.drawable.slide3,R.drawable.slide4};  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_menu);
        final ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new MenuAdapter(this,image_details));
        lv.setOnItemClickListener(this);
        actionBar = getActionBar();
        final Handler h = new Handler();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setIcon(R.drawable.ic_social_share);
        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.apptitle, null);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(v);
        int delay = 1000;
        int period = 4000;
        t1 = new Timer();
        t1.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                h.post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        animateSlideShow();
                    }
                });
            }
        }, delay, period);
    }
    private void animateSlideShow(){
        slide = (ImageView)findViewById(R.id.imagearr);
        slide.setImageResource(IMAGE_IDS[currindex%IMAGE_IDS.length]);
        currindex++;
        Animation fade = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        slide.startAnimation(fade);
    }
    private ArrayList<ItemDetails> GetSearchResults(){
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();

        ItemDetails item_details = new ItemDetails();
        item_details.setmenuitem("Featured Tours");
        item_details.setItemDescription("Mostly Recommended");
        item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setmenuitem("Theme Tours");
        item_details.setItemDescription("Some amazing experiences");
        item_details.setImageNumber(2);
        results.add(item_details);


        item_details = new ItemDetails();
        item_details.setmenuitem("Holiday Packages");
        item_details.setItemDescription("Bundles of happiness");
        item_details.setImageNumber(3);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setmenuitem("Tailor Tours");
        item_details.setItemDescription("Custommize your tours");
        item_details.setImageNumber(4);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setmenuitem("Events");
        item_details.setItemDescription("Experience the culture");
        item_details.setImageNumber(5);
        results.add(item_details);

        item_details = new ItemDetails();
        item_details.setmenuitem("Enquiry");
        item_details.setItemDescription("Ask your queries");
        item_details.setImageNumber(6);
        results.add(item_details);

        return results;
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
        return true;
    }
    @Override
    public void onItemClick(AdapterView<?> av, View v, int position, long id) {
        // TODO Auto-generated method stub
        if(position==0){
            Intent i0 = new Intent(this,FeaturedTourActivity.class);
            startActivity(i0);
        }
    else if(position==1){
            Intent i1 = new Intent(this,MainActivity.class);
            startActivity(i1);
        }
        else if(position==2){
            Intent i2 = new Intent(this,TourCatActivity.class);
            startActivity(i2);
        }
        else if(position==3){
            Intent i3 = new Intent(this,TailoredoneActivity.class);
            startActivity(i3);
        }
        else if(position==4){
            Intent i4 = new Intent(this,MainActivity.class);
            startActivity(i4);
        }
        else if(position==5){
            Intent i5 = new Intent(this,EnquireActivity.class);
            startActivity(i5);
        }
        else if(position==6){
            Intent i6 = new Intent(this,MainActivity.class);
            startActivity(i6);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
            case R.id.menu_action_search:
            {
                Intent intent_search = new Intent(this,MainActivity.class);
                startActivity(intent_search);
                break;
            }
            case R.id.menu_action_locate:
            {
                Intent intent_nearby = new Intent(this,NearbyPlacesActivity.class);
                startActivity(intent_nearby);
                break;
            }
            case R.id.menu_action_mail:
            {
                Intent intent_mail = new Intent(this,EnquireActivity.class);
                startActivity(intent_mail);
                break;
            }
            case R.id.menu_action_call:
            {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:9560875725"));
                startActivity(callIntent);
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }

}

回答1:

Create BaseActivity which implements action bar. And all your activities must inherit BaseActivity (not Activity)


public class BaseActiivity extends Activity{
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // action bar implementation
    }

}

public class MainActivity extends BaseAcivity{
    //your code
}