How to display both icon and title of action inside ActionBar?

前端 未结 10 1985
庸人自扰
庸人自扰 2020-11-29 18:08

I need to display both icon and title of action inside ActionBar.

I\'ve tried \"withText\" option, but it has no

相关标签:
10条回答
  • 2020-11-29 18:55

    Some of you guys have great answers, but I found some additional thing. If you want create a MenuItem with some SubMenu programmatically:

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            SubMenu subMenu = menu.addSubMenu(0, Menu.NONE, 0, "Menu title");
            subMenu.getItem().setIcon(R.drawable.ic_action_child);
            subMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
            subMenu.add(0, Menu.NONE, 0, "Subitem 1");
            subMenu.add(0, Menu.NONE, 1, "Subitem 2");
            subMenu.add(0, Menu.NONE, 2, "Subitem 3");
    
            return true;
        }
    
    0 讨论(0)
  • 2020-11-29 18:55

    Try adding a TextView to the menubar first and using setCompoundDrawables() to place the image on whichever side you want. Bond click activity to the textview in the end.

    MenuItem item = menu.add(Menu.NONE, R.id.menu_item_save, 10, R.string.save);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    TextView textBtn = getTextButton(btn_title, btn_image);
    item.setActionView(textBtn);
    textBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               // your selector here   }
        });
    

    You can literally customize everything here:

    public TextView getTextButton (String btn_title, Drawable btn_image) {
        TextView textBtn = new TextView(this);
        textBtn.setText(btn_title);
        textBtn.setTextColor(Color.WHITE);
        textBtn.setTextSize(18);
        textBtn.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
        textBtn.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        Drawable img = btn_image;
        img.setBounds(0, 0, 30, 30);
        textBtn.setCompoundDrawables(null, null, img, null); 
        // left,top,right,bottom. In this case icon is right to the text
    
        return textBtn;
    }
    
    0 讨论(0)
  • 2020-11-29 18:55

    Follow these steps:

    1. Add the Action Bar instance in the Java Code final ActionBar actionBar = getActionBar();
    2. Enable the Home Display Option actionBar.setDisplayShowHomeEnabled(false);
    3. Add the following code in the respective activity's manifest file android:logo=@drawable/logo and android:label="@string/actionbar_text"

    I think this will help you

    0 讨论(0)
  • 2020-11-29 18:59

    You can create actions with text in 2 ways:

    1- From XML:

    <item android:id="@id/resource_name"
          android:title="text"
          android:icon="@drawable/drawable_resource_name"
          android:showAsAction="withText" />
    

    When inflating the menu, you should call getSupportMenuInflater() since you are using ActionBarSherlock.

    2- Programmatically:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT);
        item.setIcon(R.drawable.drawable_resource_name);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    
        return true;
    }
    

    Make sure you import com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuItem.

    0 讨论(0)
提交回复
热议问题