我有3个标签,
1.Invitation tab
2.Event tab
3.Groupchat tab and
我的布局中有3个菜单,
1.Dropdown menu
2.create occasion menu
3.settings menu
现在我的所有标签显示所有三个菜单,但我需要在点击时在特定标签上显示特定菜单,例如
1.when ontap "Invitation tab" just show "settings menu" only.
2.when ontap "Event tab" only shows"create occasion and settings menus"
3.similarly when ontap "Groupchat tab" only shows"Dropdown and settings menus".
我尝试下面的代码,但它没有按预期工作。请帮我解决这个问题。
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();
private Menu menu;
@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) {
super.onCreateOptionsMenu(menu);
this.menu=menu;
getMenuInflater().inflate(R.menu.menu_user_dash_board, menu);
return true;
}
private void updateInvitationMenu() {
MenuItem dropdownMenu = menu.findItem(R.id.dropdown);
MenuItem occasionMenu=menu.findItem(R.id.create_occasion);
occasionMenu.setVisible(false);
dropdownMenu.setVisible(false);
}
private void updateEventMenu() {
MenuItem dropdownMenu = menu.findItem(R.id.dropdown);
dropdownMenu.setVisible(false);
}
private void updateGroupChatMenu() {
MenuItem eventMenu=menu.findItem(R.id.create_occasion);
eventMenu.setVisible(false);
}
@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);
//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.sandal));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.sandal));
//onTabChangedListener added for move one tab to others
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTab() == 0) {
updateInvitationMenu();
} else if (tabHost.getCurrentTab() == 1) {
updateEventMenu();
} else if (tabHost.getCurrentTab() == 2) {
updateGroupChatMenu();
}
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);
}
}
菜单代码如下,
<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>
我的信息中心布局代码如下,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
答案 0 :(得分:0)
你可以这样做
public class MainActivity extends Activity {
ToggleButton btn1, btn2, btn3;
Menu myMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ToggleButton)findViewById(R.id.option1);
btn2 = (ToggleButton)findViewById(R.id.option2);
btn3 = (ToggleButton)findViewById(R.id.option3);
btn1.setOnCheckedChangeListener(btnOnCheckedChangeListener);
btn2.setOnCheckedChangeListener(btnOnCheckedChangeListener);
btn3.setOnCheckedChangeListener(btnOnCheckedChangeListener);
}
OnCheckedChangeListener btnOnCheckedChangeListener =
new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(myMenu != null){
if(buttonView==btn1){
myMenu.findItem(R.id.menuopt1)
.setVisible(isChecked);
myMenu.findItem(R.id.menuopt1)
.setEnabled(isChecked);
}else if(buttonView==btn2){
myMenu.findItem(R.id.menuopt2)
.setVisible(isChecked);
myMenu.findItem(R.id.menuopt2)
.setEnabled(isChecked);
}else if(buttonView==btn3){
myMenu.findItem(R.id.menuopt3)
.setVisible(isChecked);
myMenu.findItem(R.id.menuopt3)
.setEnabled(isChecked);
}
}
}};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
myMenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menuopt1:
Toast.makeText(MainActivity.this,
"menuopt1",
Toast.LENGTH_SHORT).show();
break;
case R.id.menuopt2:
Toast.makeText(MainActivity.this,
"menuopt2",
Toast.LENGTH_SHORT).show();
break;
case R.id.menuopt3:
Toast.makeText(MainActivity.this,
"menuopt3",
Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}