问题描述
我创建了一个MenuActivity这是有一个操作栏和拆分操作栏。我想用这个动作条和splitactionbar查看所有活动在我的应用程序。我是新手到Android所以有人可以指导我阶段性地了解这一点。
另外,我试图把在动作条这是目前出现在SplitActionBar的搜索图标。我有SplitActionBar四个图标,我想展现的动作条上没有SplitActionBar搜索图标。搜索图标是一个搜索查看项目点击它时,扩展了动作条,这是非常凌乱。我希望它出现在动作条最右边的位置,点击时展开了相同的。
这是MenuACtivity.java:
包com.example.travelplanner;
进口的java.util.ArrayList;
进口java.util.Timer中;
进口java.util.TimerTask中;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Handler;
进口android.app.ActionBar;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.drawable.ColorDrawable;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.Window;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.SearchView;
进口android.widget.TextView;
进口com.example.travelplanner.R;
公共类MenuActivity扩展活动实现OnItemClickListener {
定时器T1;
TimerTask的TT1;
ImageView的幻灯片;
INT currindex = 0;
动作条动作条;
ArrayList的< ItemDetails> image_details = GetSearchResults();
私人诠释IMAGE_IDS [] = {R.drawable.slide1,R.drawable.slide2,R.drawable.slide3,R.drawable.slide4};
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
的setContentView(R.layout.activity_menu);
最终的ListView LV =(ListView控件)findViewById(R.id.listView1);
lv.setAdapter(新MenuAdapter(这一点,image_details));
lv.setOnItemClickListener(本);
动作条= getActionBar();
最终的处理程序H =新的处理程序();
actionBar.setDisplayHomeAsUpEnabled(真正的);
actionBar.setDisplayShowCustomEnabled(真正的);
actionBar.setIcon(R.drawable.ic_social_share);
LayoutInflater充气=(LayoutInflater)本
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
视图V = inflator.inflate(R.layout.apptitle,NULL);
actionBar.setDisplayShowTitleEnabled(假);
actionBar.setCustomView(五);
INT延迟= 1000;
INT周期= 4000;
T1 =新的Timer();
t1.scheduleAtFixedRate(新的TimerTask(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
h.post(新的Runnable(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
animateSlideShow();
}
});
}
},延迟,周期);
}
私人无效animateSlideShow(){
滑动=(ImageView的)findViewById(R.id.imagearr);
slide.setImageResource(IMAGE_IDS [currindex%IMAGE_IDS.length]);
currindex ++;
动画淡出= AnimationUtils.loadAnimation(这一点,android.R.anim.fade_in);
slide.startAnimation(淡出);
}
私人的ArrayList< ItemDetails> GetSearchResults(){
ArrayList的< ItemDetails>结果=新的ArrayList< ItemDetails>();
ItemDetails item_details =新ItemDetails();
item_details.setmenuitem(特色旅游);
item_details.setItemDescription(大部分建议);
item_details.setImageNumber(1);
results.add(item_details);
item_details =新ItemDetails();
item_details.setmenuitem(主题旅游);
item_details.setItemDescription(一些令人惊讶的经验);
item_details.setImageNumber(2);
results.add(item_details);
item_details =新ItemDetails();
item_details.setmenuitem(自由行);
item_details.setItemDescription(丛的幸福);
item_details.setImageNumber(3);
results.add(item_details);
item_details =新ItemDetails();
item_details.setmenuitem(裁缝之旅);
item_details.setItemDescription(Custommize你游);
item_details.setImageNumber(4);
results.add(item_details);
item_details =新ItemDetails();
item_details.setmenuitem(事件);
item_details.setItemDescription(体验文化);
item_details.setImageNumber(5);
results.add(item_details);
item_details =新ItemDetails();
item_details.setmenuitem(查询);
item_details.setItemDescription(问问你的查询);
item_details.setImageNumber(6);
results.add(item_details);
返回结果;
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.menu,菜单);
搜索查看搜索查看=(搜索查看)menu.findItem(R.id.menu_action_search).getActionView();
返回true;
}
@覆盖
公共无效onItemClick(适配器视图<> AV,视图V,INT位置,长的id){
// TODO自动生成方法存根
如果(位置== 0){
意图I0 =新的意图(这一点,FeaturedTourActivity.class);
startActivity(I0);
}
否则,如果(位置== 1){
意图I1 =新的意图(这一点,MainActivity.class);
startActivity(I1);
}
否则,如果(位置== 2){
意图I2 =新的意图(这一点,TourCatActivity.class);
startActivity(I 2);
}
否则,如果(位置== 3){
意图I3 =新的意图(这一点,TailoredoneActivity.class);
startActivity(I3);
}
否则,如果(位置== 4){
意图I4 =新的意图(这一点,MainActivity.class);
startActivity(I4);
}
否则,如果(位置== 5){
意图I5 =新的意图(这一点,EnquireActivity.class);
startActivity(I5);
}
否则,如果(位置== 6){
意图I6 =新的意图(这一点,MainActivity.class);
startActivity(I6);
}
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
// TODO自动生成方法存根
开关(item.getItemId()){
案例R.id.menu_action_search:
{
意图intent_search =新的意图(这一点,MainActivity.class);
startActivity(intent_search);
打破;
}
案例R.id.menu_action_locate:
{
意图intent_nearby =新的意图(这一点,NearbyPlacesActivity.class);
startActivity(intent_nearby);
打破;
}
案例R.id.menu_action_mail:
{
意图intent_mail =新的意图(这一点,EnquireActivity.class);
startActivity(intent_mail);
打破;
}
案例R.id.menu_action_call:
{
意图callIntent =新的意图(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(电话:9560875725));
startActivity(callIntent);
打破;
}
}
返回super.onOptionsItemSelected(项目);
}
}
创建 BaseActivity
它实现行动起来吧
。和所有的活动都必须继承 BaseActivity
(而不是活动
)
公共类BaseActiivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//操作栏执行
}
}
公共类MainActivity扩展BaseAcivity {
//你的code
}
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);
}
}
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
}
这篇关于在应用程序的所有活动重用操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!