简体   繁体   中英

Android: ActionBar (Android support library)

i have a problem with ActionBarCompat (from support library). I have no idea how I can add a few buttons on ActionBar where I drew black circle (on screenshot) together with menu in the left of the screen.

Please, I need help!

在此处输入图片说明

Code of menu in ActionBar in the left of the screen.

     private DrawerLayout mDrawerLayout;
    private ListView mDrawer;
    private ActionBarHelper mActionBar;
    private ActionBarDrawerToggle mDrawerToggle;

...

        linearLayout = (LinearLayout)findViewById(R.id.fragment_container);
        linearLayout.setId(LAYOUT_ID);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawer = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerListener(new DDrawerListener());
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

...
        fragments = new  Fragment[NUMBER_OF_TABS];
        mDrawer.setAdapter(new CustomAdapter(this));
        mDrawer.setOnItemClickListener(new DrawerItemClickListener());


        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
        actionBar.setDisplayHomeAsUpEnabled(true);


        mActionBar = createActionBarHelper();
        mActionBar.init();


        mDrawer.setBackgroundColor(Color.parseColor("#e5c391"));
        mDrawer.setCacheColorHint(Color.parseColor("#e5c391"));

        initArrays(this);

        mDrawerToggle = new ActionBarDrawerToggle
        (this, mDrawerLayout,R.drawable.ic_drawer,R.string.app_drawer_open, R.string.app_drawer_close);

        if (savedInstanceState == null)
        {
            addFragment(0);
        }

Welcome other ways too =) Thank you! Happy New Year!

From https://developer.android.com/training/basics/actionbar/adding-buttons.html :

Specify the Actions in XML

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory.

Add an element for each item you want to include in the action bar. For example:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

Add the Actions to the ActionBar

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

Respond to Action Buttons

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

请在Android Developer门户中参考此注释

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM