Android ActionBar - Push custom view to bottom of screen

前端 未结 2 745
无人共我
无人共我 2021-01-30 12:02

I\'ve got a split ActionBar, and I\'m trying to add functionality almost identical to google play\'s \'Now playing\'..

I can get menu items to appear at the bottom of th

2条回答
  •  我在风中等你
    2021-01-30 12:33

    I've done a solution based on yours, including custom layout on top + custom layout on bottom + title/subtitle + home icon. Here is the code:

    public class SplitABActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ActionBar ab = getActionBar();
        ab.setTitle("Action Bar Title");
        ab.setSubtitle("Action Bar Subtitle");
        ab.setDisplayShowHomeEnabled(true);
    
        LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.ab_custom_layout, null);
        ab.setCustomView(layout);
        ab.setSplitBackgroundDrawable(new ColorDrawable(Color.BLUE));
        ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_including_layout, menu);
        RelativeLayout layout = (RelativeLayout) menu.findItem(R.id.layout_item).getActionView();
        View v = getLayoutInflater().inflate(R.layout.ab_bottom_layout, null);
        layout.addView(v); 
    
        return super.onCreateOptionsMenu(menu);
    }
    }
    

    ab_custom_layout.xml

    
       
    
       
    
    
    

    menu_including_layout.xml

    
    
    
    
    
    
    
     
    

    ab_bottom_layout.xml

    
    
    
    
    
    
    

    Code works perfect for me. I can see the split ActionBar when my tablet is on portrait. On landscape it is wide enough to store all the items on the top.

    Hope it helps

提交回复
热议问题