我正在开发一个应用程序,其中我有四个标签。我希望点击每个标签,操作栏应该更改。现在我已经给了操作栏标题,搜索栏和溢出图标。但我想改变行动选择每个标签栏,以便我可以根据相应标签选择的要求在操作栏中添加图标。
这是我的ontabselection代码,请建议我该怎么做。任何帮助都是可以接受的。
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fragMentTra;
if (tab.getText().equals("DEALS")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Deals");
FragmentDeals Fram1 = new FragmentDeals();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram1);
fragMentTra.commit();
}
else if (tab.getText().equals("ORDER")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Order");
FragmentOrder Fram2 = new FragmentOrder();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram2);
fragMentTra.commit();
}
else if(tab.getText().equals("CART")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("Shopping Cart");
FragmentCart fram3 = new FragmentCart();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram3);
fragMentTra.commit();
}
else if(tab.getText().equals("HISTORY")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setTitle("History");
FragmentHistory fram4 = new FragmentHistory();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram4);
fragMentTra.commit();
}
}
感谢。
答案 0 :(得分:2)
我看到你正在使用片段来制作标签。很容易,您可以为每个片段声明一个menu.xml文件。在片段的oncreate中使用sethasOptionmenu(true)。然后你可以在onCreate Option菜单()中根据需要调整内容。
编辑:这里有一个例子:
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
setHasOptionsMenu(true);
actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
}
@Override
public void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
if (!hideOptionsMenu) {
Log.e(TAG, " - onCreateOPTIONMenu");
inflater.inflate(R.menu.booking_accepted, menu);
MenuItem cancel = menu.findItem(R.id.cancel_booking_accepted);
cancel.getIcon().setAlpha(Constants.ENABLED_OPACITY);
menu.findItem(R.id.my_trips_switch).getIcon().setAlpha(Constants.ENABLED_OPACITY);
}
// (optional) reference to a OnItemSelectedListener, that you can use to perform actions based on user selection
}
答案 1 :(得分:0)
与Poutrathor类似,但对我来说更清楚。我从这里得到了它:https://stackoverflow.com/a/15218201/1887635
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_xxx, menu);
}