当前位置 : 主页 > 手机开发 > android >

android – ActionBar Icon在setVisible为false后再次出现在ActionBar上,然后才为true

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个ActionBar模式ActionBar.NAVIGATION_MODE_TABS,当我选择不同的标签时,我想更改ActionBar上显示的图标. 我使用Activity成功完成了它,但是当我再次在另一个Activity上应用它时,它表现得很奇怪
我有一个ActionBar模式ActionBar.NAVIGATION_MODE_TABS,当我选择不同的标签时,我想更改ActionBar上显示的图标.

我使用Activity成功完成了它,但是当我再次在另一个Activity上应用它时,它表现得很奇怪.

假设我有两个标签:Tab1和Tab2,我想在选择Tab2时显示一个Camera图标,并在选择Tab1时消失.而且,Tab1是活动开始时将选择的第一个选项卡.
所以我做了这样的事情:

因为我不想在HomeActivity第一次启动时出现相机图标,所以我设置android:visible =“false”,然后当我在代码下面运行时:当HomeActivity启动时,相机图标不会出现在ActionBar上 – &gt ;正确,但是当我切换到tab2时,摄像头图标也没有出现在ActionBar上,但如果我按下“菜单”按钮 – > MenuItem是“Camera”,显示为溢出操作栏项.

如果我设置android:visible =“true” – >除了第一次HomeActivity开始时第一次出现相机图标(不正确!!!) – >当我更改tab1 / tab2时,相机图标会消失/正确显示.

任何人都可以解释并给我一个解决方案,使其正常工作.
谢谢!

这是我的代码:

actionbar_home.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/actionbar_home_camera"
        android:title="@string/actionbar_rc_camera"
        android:icon="@drawable/ic_action_camera"
        android:showAsAction="always" 
        android:visible="false"/>
</menu>

HomeActivity.java

public class HomeActivity extends SherlockFragmentActivity {
private ActionBar actionBar;
private Menu menu;
private Tab1Fragment tab1Fragment;
private Tab2Fragment tab2Fragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    this.configActionBar();
    this.createTabs();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.actionbar_home, menu);
    this.menu = menu;

    // pass objects to tabs
    tab1Fragment.setOptionMenu(this.menu);
    tab2Fragment.setOptionMenu(this.menu);

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}

private void configActionBar()
{
    actionBar = this.getSupportActionBar();
    // below line makes application icon in top-left corner become a button
    actionBar.setHomeButtonEnabled(true);
    // below line add an arrow "<" before the top-left icon
    //ab.setDisplayHomeAsUpEnabled(true);
    //actionBar.setDisplayShowTitleEnabled(true);
}

private void createTabs()
{
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tab1 = actionBar.newTab().setText(
            Const.TAB_TITLE_HOME_TAB1);
    ActionBar.Tab tab2 = actionBar.newTab().setText(
            Const.TAB_TITLE_HOME_TAB2);

    // create the two fragments we want to use for display content
    tab1Fragment = new Tab1Fragment();
    tab2Fragment = new Tab2Fragment();        

    // add listener
    tab1.setTabListener(
            new HomeTabListener(tab1Fragment));
    tab2.setTabListener(
            new HomeTabListener(tab2Fragment));        
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);     
}
}

BaseTabFragment.java

public class BaseTabFragment extends SherlockFragment{
    protected View root;
    protected Menu optionMenu;
    protected SherlockFragmentActivity container;

    public Menu getOptionMenu() {
        return optionMenu;
    }
    public void setOptionMenu(Menu optionMenu) {
        this.optionMenu = optionMenu;
    }
    public SherlockFragmentActivity getContainer() {
        return container;
    }
    public void setContainer(SherlockFragmentActivity container) {
        this.container = container;
    }
}

Tab1Fragment.java

public class Tab1Fragment extends BaseTabFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.tab1_layout, 
                container, false);


        this.changeActionBarLayout();

        return root;
    }

    private void changeActionBarLayout()
    {
        try {
            this.optionMenu.findItem(R.id.actionbar_home_camera)
                .setVisible(false);
        } catch (NullPointerException e)
        {
            // e will be thrown at the first time MainActivity is called
        }       
    }
}

Tab2Fragment.java

public class Tab2Fragment extends BaseTabFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.tab2_layout, 
                container, false);


        this.changeActionBarLayout();

        return root;
    }

    private void changeActionBarLayout()
    {
        try {
            this.optionMenu.findItem(R.id.actionbar_home_camera)
                .setVisible(true);
        // Camera Icon still doesn't appear on ActionBar even when I set it as SHOW_AS_ACTION_ALWAYS
        this.optionMenu.findItem(R.id.actionbar_home_camera)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        } catch (NullPointerException e)
        {
            // e will be thrown at the first time MainActivity is called
        }       
    }   
}
我遇到了同样的问题并找到了一个简单的解决方案:只需在Activity中实现onPreparePanel,即使所有项都不可见,也返回true.

public class HomeActivity extends SherlockFragmentActivity {

    [...]

    @Override
    public boolean onPreparePanel(int featureId, View view, Menu menu) {
        super.onPreparePanel(featureId, view, menu); // this returns false if all items are hidden
        return true; // return true to prevent the menu's deletion 
    }

    [...]

}
网友评论