自定义 AppCompat 操作栏

Customizing the AppCompat Actionbar

有人问过这个问题,但不是真的,我想做的是为我们的应用程序提供一个高度自定义的工具栏,我最近用 AppCompat Actionbar 实现了一个抽屉菜单,效果很好,除了这个是它默认给我的:

我想要的是这样的东西(请原谅匆忙的绘图):

我完全有能力在正常布局中执行此操作,所以我不是在寻求有关如何使工具栏看起来像这样的建议,我的问题是,我如何开始自定义布局工具栏?例如,我看不到放置主页按钮的方法(我相信在这种情况下汉堡菜单就是这样叫的),似乎很多这样的东西都内置在 appcompat 操作栏中,但我看不到一种绕过它的方法,基本上总结一下,我只想在工具栏中有多行控件,并自定义工具栏的默认内置控件,包括定位。有没有一种方法可以将我自己的布局文件插入到操作栏中,并将控件设置为激活抽屉布局抽屉的按钮?对于我的情况,这将是解决此问题的最理想的解决方案。

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

您可以像这样自定义 SupportActionbar 的视图:

  1. 为你的Actionbar创建一个ToolbarView.axml视图文件(这只是一个例子,你可以在里面定义任何东西):

    <?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. 在您的 activity 的 onCreate 方法中,设置 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. ActionBar的高度不会因为里面的内容而变化,需要手动设置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>