How can I make the items on ActionBar to be one on the left, one in the center and one on the right?

前端 未结 2 1964
萌比男神i
萌比男神i 2021-01-31 11:37

I\'m using actionbarsherlock to do it. Example of what I want in the actionbar:
[LOGIN]     [COMPANY LOGO]    &nb

2条回答
  •  误落风尘
    2021-01-31 12:21

    You should create custom view for that. For example (layout/ab_custom.xml):

    
    
    
        
    
        
    
        
    
    
    

    Then in your Activity's onCreate call this method:

    private void showActionBar() {
            LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.ab_custom, null);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled (false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(v);
    }
    

    To take control on your items use this:

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.btn1:
                //left button, do something
                return true;
            case R.id.btn2:
                //center button
                return true;
            case R.id.btn3:
                // right button
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    

    EDIT: revised my answer. It's better to use RelativeLayout as parent.

提交回复
热议问题