简体   繁体   English

自定义AppCompat操作栏

[英]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: 这个问题已经被问到了,但不是真的,我想要做的是为我们的应用程序提供一个高度自定义的工具栏,我最近使用AppCompat Actionbar实现了一个抽屉菜单,并且效果很好,除了它能给我带来什么默认:

在此处输入图片说明

What I'd like to have is something like this (excuse the rushed drawing): 我想拥有的是这样的东西(借来的图纸): 在此处输入图片说明

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. 例如,我看不到放置主页按钮的方法(我相信在这种情况下就是所谓的hambuger菜单),似乎很多东西都内置在appcompat操作栏中,但我看不到一种解决它的方法,基本上是总结一下,我只想在工具栏中有多行控件,并自定义工具栏的默认内置控件,包括定位。 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. 对于我的情况,这将是解决此问题的最理想解决方案。

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: 您可以像这样自定义SupportActionbar的视图:

  1. Create a ToolbarView.axml view file for your Actionbar(It's just an example, you can define anything inside): 为您的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. In your activity's onCreate method, set the custom view of the SupportActionbar : 在活动的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. The height of the ActionBar won't change by the content inside, you have to set the height manually in Style.xml : 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM