Trevor Hart
Trevor Hart

Reputation: 1023

Customizing the AppCompat Actionbar

This question has sort of been asked but not really, what I'm looking to do is have a highly customized toolbar for our app, I recently implemented a drawer menu with the AppCompat Actionbar and it works great, except this is what it gives me by default:

enter image description here

What I'd like to have is something like this (excuse the rushed drawing): enter image description here

I'm prefectly capable of doing this in a normal layout, so I'm not looking for advice on how to make the toolbar look like this, what my question is, is how do I start customizing the layout of the toolbar at all? For example, I don't see a way to position the home button (I believe that's what the hambuger menu is called in this case), it seems like a lot of this stuff is built into the appcompat actionbar and I don't see a way of getting around it, basically to summarize, I just want to have multiple rows of controls in the toolbar, and customize the default built in controls of the toolbar, including positioning. Is there a way I can just insert my own layout file into the actionbar and just have a control set to be the button that activates the drawer layouts drawers? That would be the most ideal solution to this problem for my circumstances.

Upvotes: 0

Views: 567

Answers (1)

Elvis Xia - MSFT
Elvis Xia - MSFT

Reputation: 10831

what my question is, is how do I start customizing the layout of the toolbar at all?

You can customize the view of your SupportActionbar like this:

  1. Create a ToolbarView.axml view file for your Actionbar(It's just an example, you can define anything inside):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp">
    
      <TextView
        android:id="@+id/tvShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Please Click Me"/>
    
      <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    
  2. In your activity's onCreate method, set the custom view of the SupportActionbar:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        SupportActionBar.SetDisplayShowCustomEnabled(true);
        SupportActionBar.SetCustomView(Resource.Layout.ToolbarView);
        ...
     }
    
  3. The height of the ActionBar won't change by the content inside, you have to set the height manually in Style.xml:

    <resources>
      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#5A8622</item>
        <!--Set the ActionBar Size-->
        <item name="actionBarSize">200dp</item>
      </style>
    </resources>
    

Upvotes: 1

Related Questions