我正在开发一个Android应用程序,我需要根据标签点击功能隐藏/菜单可见的菜单项。当ontap时,
1.invitation_tab"设置图标"仅在菜单板中可见
2.当ontap event_tab" create_occasion icon"仅在菜单板中可见
3.类似于ontap Groupchat_tab"下拉图标"仅在菜单板中可见。
如何实现这一点,请帮助我。
我的布局如下所示,
菜单编码如下,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ringee.app.com.ringeeapp.UserDashBoard">
<item
android:id="@+id/dropdown"
android:icon="@drawable/dropdown_icon"
android:title="Dropdown"
appmunu:showAsAction="always">
<menu>
<item
android:id="@+id/all"
android:title="All" />
<item
android:id="@+id/event"
android:title="Event" />
<item
android:id="@+id/invitation"
android:title="Invitation" />
</menu>
</item>
<item
android:id="@+id/create_occasion"
android:icon="@drawable/ic_action_event"
android:title="Create Occasion"
appmunu:showAsAction="always" />
<item
android:id="@+id/account_settings"
android:orderInCategory="100"
android:title="@string/action_settings" />
<item
android:id="@+id/profile"
android:orderInCategory="100"
android:title="@string/profile_image" />
</menu>
我的活动编码如下,
public class UserDashBoardActivity extends ActionBarActivity {
/** Called when the activity is first created. */
private static final String TAB_1_TAG = "Invitation";
private static final String TAB_2_TAG = "Event";
private static final String TAB_3_TAG = "GroupChat";
private FragmentTabHost tabHost;
private Context context;
private SharedPreferences sharedpreferences;
private Gson gson = new Gson();
@Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
@Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
@Override
protected void onResume() {
super.onPause();
AppActivityStatus.setActivityStarted();
}
@Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user_dash_board, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
Context.MODE_PRIVATE);
// Get TabHost Refference
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("Invitation"), InvitationFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("Event"), OccasionFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("GroupChat"), GroupChatFragment.class, null);
// Set drawable images to tab
/*tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);*/
//invitation tab highlighted by default
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.Orange));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.scandal));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.scandal));
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
setTabColor(tabHost);
}
});
}
//setTabColor method added for highlighting the tabs
public void setTabColor(FragmentTabHost tabHost) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(R.color.scandal));//unselected
if(tabHost.getCurrentTab()==0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //1st tab selected
else if(tabHost.getCurrentTab()==1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //2nd tab selected
else if(tabHost.getCurrentTab()==2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //3rd tab selected
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// noinspection SimplifiableIfStatement
if (id == R.id.account_settings) {
Intent userSettingIntent = new Intent(getApplicationContext(),ActivityUserSettings.class);
userSettingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(userSettingIntent);
return true;
}
if (id == R.id.profile) {
Intent profileIntent = new Intent(getApplicationContext(),ImageUploadActivity.class);
profileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(profileIntent);
return true;
}
if(id == R.id.create_occasion){
Intent occasionAct = new Intent(getApplicationContext(), OccasionActivity.class);
// Clears History of Activity
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我建议将活动中的引用保存到您在onCreateOptionsMenu
中收到的Menu对象,然后使用它来检索需要在需要时进行更改的MenuItem
。例如,您可以执行以下操作:
public class YourActivity extends Activity {
private Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Create your menu...
this.menu = menu;
getMenuInflater().inflate(R.menu.options, menu);
return true;
}
private void updateMenuTitles() {
MenuItem bedMenuItem = menu.findItem(R.id.actionbar_notifcation);
bedMenuItem.setVisible(View.Invisible);
}
}
或者,您可以覆盖onPrepareOptionsMenu
以在每次显示菜单时更新菜单项。