如何从 Android appcompat v7 21 库中设置 DrawerArrowToggle 的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26439572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to style the DrawerArrowToggle from Android appcompat v7 21 library
提问by Bignadad
So now that Android 5.0 was released i was wondering how to style the animated actionbar icons.
所以现在 Android 5.0 发布了,我想知道如何设置动画操作栏图标的样式。
This library hereimplements and styles it fine for me but since the appcompat v7 library has it how can it be styled?
这里的这个库对我来说很好地实现和样式化,但是既然 appcompat v7 库有它,它怎么能被样式化呢?
I got this implemented using the v7 DrawerToggle. However I cannot style it. Please Help
我使用 v7 DrawerToggle 实现了这个。但是我不能设计它。请帮忙
I found the styling for it in the v7 styles_base.xml
我在 v7 styles_base.xml 中找到了它的样式
<style name="Base.Widget.AppCompat.DrawerArrowToggle" parent="">
<item name="color">?android:attr/textColorSecondary</item>
<item name="thickness">2dp</item>
<item name="barSize">18dp</item>
<item name="gapBetweenBars">3dp</item>
<item name="topBottomBarArrowSize">11.31dp</item>
<item name="middleBarArrowSize">16dp</item>
<item name="drawableSize">24dp</item>
<item name="spinBars">true</item>
</style>
I added this to my styles and did not work. Also added to my attr.xml
我将此添加到我的样式中,但不起作用。也添加到我的 attr.xml
<declare-styleable name="DrawerArrowToggle">
<!-- The drawing color for the bars -->
<attr name="color" format="color"/>
<!-- Whether bars should rotate or not during transition -->
<attr name="spinBars" format="boolean"/>
<!-- The total size of the drawable -->
<attr name="drawableSize" format="dimension"/>
<!-- The max gap between the bars when they are parallel to each other -->
<attr name="gapBetweenBars" format="dimension"/>
<!-- The size of the top and bottom bars when they merge to the middle bar to form an arrow -->
<attr name="topBottomBarArrowSize" format="dimension"/>
<!-- The size of the middle bar when top and bottom bars merge into middle bar to form an arrow -->
<attr name="middleBarArrowSize" format="dimension"/>
<!-- The size of the bars when they are parallel to each other -->
<attr name="barSize" format="dimension"/>
<!-- The thickness (stroke size) for the bar paint -->
<attr name="thickness" format="dimension"/>
</declare-styleable>
But crashes and says color type error when doing so. What am i missing?
但是这样做时会崩溃并说颜色类型错误。我错过了什么?
回答by Chris Banes
The following works for me:
以下对我有用:
<style name="MyTheme" parent="Theme.AppCompat">
<item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
</style>
<style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/your_color</item>
</style>
回答by marwinXXII
In my case I wanted to change color of drawer arrow and hamburger icon. Setting drawer arrow style changed only hamburger icon color.
就我而言,我想更改抽屉箭头和汉堡包图标的颜色。设置抽屉箭头样式仅更改汉堡包图标颜色。
So I opened Widget.AppCompat.DrawerArrowToggle
style in values.xml
of appcompat-v7.
所以我打开了 appcompat-v7 的Widget.AppCompat.DrawerArrowToggle
样式values.xml
。
<style name="Widget.AppCompat.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">?attr/colorControlNormal</item>
</style>
So I created special theme:
所以我创建了一个特殊的主题:
<style name="Toolbar_Theme">
<item name="colorControlNormal">@android:color/black</item>
</style>
and applied it to my toolbar as following:
并将其应用到我的工具栏,如下所示:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:theme="@style/Toolbar_Theme" />
Note, that I'm using theme
attributeinstead of defining controlColorNormal
in my app theme. This way color will aply only to toolbar items, if I set it in app theme, then it will also affect scroll bars color, etc.
请注意,我使用的是theme
属性而不是controlColorNormal
在我的应用程序主题中定义。这样颜色只会应用于工具栏项目,如果我在应用程序主题中设置它,那么它也会影响滚动条颜色等。
Setting colorControlNormal
attribute change both color of hamburger and drawer arrow.
设置colorControlNormal
属性更改汉堡包和抽屉箭头的颜色。
回答by Mateus Gondim
For anyone that ends up here (like I did) looking for a way to replace the drawer indicator icon with your own drawable(non animated) using the v7 ActionBarDrawerToggle, you can do the following:
对于最终到这里(就像我一样)寻找一种使用 v7 ActionBarDrawerToggle 用您自己的可绘制(非动画)替换抽屉指示器图标的方法的人,您可以执行以下操作:
//After instantiating your ActionBarDrawerToggle
mDrawerToggle.setDrawerIndicatorEnabled(false);
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme());
mDrawerToggle.setHomeAsUpIndicator(drawable);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
回答by softwaresupply
For my case the theming of the actionbar was relevant to switch between a white and a dark hamburger icon:
就我而言,操作栏的主题与在白色和黑色汉堡包图标之间切换有关:
<item name="actionBarWidgetTheme">@style/Theme.AppCompat</item>
vs.
对比
<item name="actionBarWidgetTheme">@style/Theme.AppCompat.Light</item>
回答by Ernesto Vega
I wanted to make the hamburguer/arrow white, but nothing of all this "styles" stuff worked for me. I removed all and adding this linejust solve it, as an attribute of the "the android.support.v7.widget.Toolbar" element in the layout:
我想把汉堡包/箭头变成白色,但所有这些“风格”的东西都不适合我。我删除了所有并添加此行只是解决它,作为布局中“android.support.v7.widget.Toolbar”元素的属性:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar
回答by Stefano
Your style name has no parent Here's working with android 5 and Material Theme, without need to replace actionbar with appCompat Here's my style, i just needed to change color to white because i had a coloured bar
您的样式名称没有父级 这是使用 android 5 和 Material Theme,无需用 appCompat 替换 actionbar 这是我的样式,我只需要将颜色更改为白色,因为我有一个彩色条
<style name="myTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/white</item>
</style>
And here's my theme
这是我的主题
<style name="Theme_AppTheme.Light" parent="@android:style/Theme.Material.Light">
<!-- Drawer\arrow in white -->
<item name="drawerArrowStyle">@style/myTheme.DrawerArrowToggle</item>
ps. there's just a problem, on adroid 4.4 and normal actionbar is not rendering well. And unluckily the nre drawerToggle from v7 does not inherit from v4
附:只是有一个问题,在 android 4.4 上,正常的操作栏不能很好地呈现。不幸的是,v7 中的 nre drawerToggle 不是从 v4 继承的
回答by Riyaz Mohammed Ibrahim
Not sure what's the exact error, if you get color already defined, go to attrs.xml search for attr name="color" and rename it or for test comment it, you might need to change your custom attr to someother name, not sure this is an support lib issue, this works for me
不确定确切的错误是什么,如果您已经定义了颜色,请转到 attrs.xml 搜索 attr name="color" 并重命名它或进行测试注释,您可能需要将自定义 attr 更改为其他名称,不确定这是一个支持库问题,这对我有用