在应用程序的所有活动中重复使用Action Bar



我创建了一个MenuActivity,它有一个Action Bar和一个Split Action Bar。我想将这个actionbar和splitactionbar视图用于我的应用程序中的所有活动。我是安卓系统的新手,所以有人可以逐步指导我。

此外,我正在尝试将搜索图标放在操作栏上,它现在出现在SplitActionBar上。我在SplitActionBar上有四个图标,我想在actionbar上显示搜索图标,而不是在SplitAction上。搜索图标是一个SearchView项目,单击后会在ActionBar上展开,ActionBar非常不整洁。我希望它出现在ActionBar中最右边的位置,并在单击时展开。

这是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);
    }
}

创建实现action barBaseActivity。并且您的所有活动都必须继承BaseActivity(而不是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
}

相关内容

  • 没有找到相关文章

最新更新