如何禁用时的背景颜色HomeUp是pressed? [英] How to disable the background color when HomeUp is pressed?
问题描述
背景:
我目前启用活动
,由于 HomeUp
键:
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
根据<一href="http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled%28boolean%29"相对=nofollow>文档,它涉及到<一个href="http://developer.android.com/reference/android/app/ActionBar.html#setHomeButtonEnabled%28boolean%29"相对=nofollow> setHomeButtonEnabled
其中:
启用或禁用操作栏的角落家按钮[...]设置不变的 DISPLAY_HOME_AS_UP 的显示选项的(setDisplayHomeAsUpEnabled方法)的将自动启用home键。
另外,我定制了选择
项目的动作条
国家背景,这种风格:
&LT;样式名称=MainTheme父=@风格/ Theme.AppCompat.Light&GT;
&LT;项目名称=机器人:actionBarItemBackground&GT; @可绘制/ ab_item_background&LT; /项目&GT;
&LT;项目名称=actionBarItemBackground&GT; @可绘制/ ab_item_background&LT; /项目&GT;
&LT; /风格&GT;
虽然这工作得很好,我不希望使用绘制选择
为 HomeUp
按钮时 UpIndicator
已启用。
为什么我需要它:
ActionBar的背景为灰色(相对于 Theme.AppCompat.Light
),并选择使用的绿色背景,当项目是pressed。然而,应用程序图标也是绿色,当它是pssed $ P $,汇合于pssed背景$ P $状态。我需要通过不使用绘制的HomeUp的背景,以避免这种情况。
问:
我怎样才能做到这一点,有关解决办法的任何想法?
注:的
-
我试图寻找一个相关的
属性
,它允许使用自定义背景的应用程序图标和UpIndicator,在自定义style.xml
但我没有找到它的资源的。 -
如果我用
setDisplayHomeAsUpEnabled(真)
和setDisplayHomeAsUpEnabled(假)
,我有预期的行为对于菜单项(背景改为相对于它的状态),但HomeUp按钮,但我有它的背景正确的行为,将不再启用。 -
另外,我想避免使用
CustomView
的动作条。
更新:
根据 Adneal的回答以及测试和研究后,这里是结果:
_______________________________________________________________________________
| | | | |
| API | R.id.home | android.R.id.home |方法|
| _____ | ________________________ | _________________________ | ______________________ |
| | | | |
| 8 | OK | - |的getParent()×1 |
| 10 | OK | - |的getParent()×1 |
| 11 | OK | - |的getParent()×1 |
| 12 | OK | - |的getParent()×1 |
| 13 | OK | - |的getParent()×1 |
| 14 | - | OK |的getParent()×1 |
| 15 | - | OK |的getParent()×1 |
| 16 | - | OK |的getParent()×1 |
| 17 | - | OK |的getParent()×2 |
| 18 | - | OK |的getParent()×2 |
| 19 | - | OK |的getParent()×2 |
| _____ | ________________________ | _________________________ | ______________________ |
然后,正确的条件应该是:
如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.JELLY_BEAN_MR1){
//获得两个父母android.R.id.home
}否则如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
//获得第一个亲以android.R.id.home
} 其他 {
//获得第一个亲以R.id.home
}
<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/action_bar_home.xml#L28"相对=nofollow>这是一个包含了home键,一次充气的<一个布局href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/ActionBarView.java#L198"相对=nofollow> ActionBarView
,被添加到<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/action_bar_up_container.xml#L20"相对=nofollow>高达容器布局这是实际使用 actionBarItemBackground
属性。
不过,<一个href="https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/src/android/support/v7/internal/widget/ActionBarView.java#L218"相对=nofollow> AppCompat
不使用一个向上的容器的布局,以及正常的动作条
高达API级别18(果冻豆MR2)。
所以,要改变家庭按钮的背景,你会成为一个电话到 View.getParent
使用支持库或API等级11-17的动作条
,而是两个,当你改变空气污染指数18 +的。
查看家;
最终诠释版本= Build.VERSION.SDK_INT;
如果(版本&GT; = Build.VERSION_ codeS.JELLY_BEAN_MR2){
//正常操作栏,后了集装箱
家庭=(查看)findViewById(android.R.id.home).getParent()的getParent()。
}否则如果(版本&GT; = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
//正常行动起来吧,pre型了集装箱
家庭=(查看)findViewById(android.R.id.home).getParent();
} 其他 {
//支持库行动起来吧
家庭=(查看)findViewById(R.id.home).getParent();
}
home.setBackgroundDrawable(空);
AppCompat
可能在将来被更新,以包括一个向上的容器的布局,所以记住,你可能需要以后调整code到适应这一点。
Background:
I currently enable the HomeUp
button on Activity
, thanks to:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
According to the Documentation, it's related to setHomeButtonEnabled
which:
Enables or disables the "home" button in the corner of the action bar [...] Setting the constant DISPLAY_HOME_AS_UP display option (setDisplayHomeAsUpEnabled method) will automatically enable the home button.
Also, I customize with a selector
the state background of items in ActionBar
with this style:
<style name="MainTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
<item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>
Although this works well, I don't want to use a drawable selector
for the HomeUp
button when UpIndicator
is enabled.
Why do I need it:
The ActionBar's background is grey (relative to Theme.AppCompat.Light
) and the selector uses a green background when items are pressed. However, the app icon is also green which, when it is pressed, merges with the background pressed state. I need to avoid this by not using a drawable for HomeUp's background.
Question:
How can I achieve this, any idea about a workaround?
Notes:
I tried to search a related
attribute
, which allows to use a custom background for the app icon and UpIndicator, to customize instyle.xml
but I didn't find it in the Resources.If I use
setDisplayHomeAsUpEnabled(true)
andsetDisplayHomeAsUpEnabled(false)
, I have the expected behaviour for the Menu Items (backgrounds changed relative to its states) but the HomeUp button, although I have the right behaviour with its background, is no longer enabled.Also, I want to avoid using a
CustomView
to the ActionBar.
Update:
According to Adneal's answer and after tests and researches, here is the results:
_______________________________________________________________________________
| | | | |
| API | R.id.home | android.R.id.home | method |
|_____|________________________|_________________________|______________________|
| | | | |
| 8 | OK | -- | getParent() x 1 |
| 10 | OK | -- | getParent() x 1 |
| 11 | OK | -- | getParent() x 1 |
| 12 | OK | -- | getParent() x 1 |
| 13 | OK | -- | getParent() x 1 |
| 14 | -- | OK | getParent() x 1 |
| 15 | -- | OK | getParent() x 1 |
| 16 | -- | OK | getParent() x 1 |
| 17 | -- | OK | getParent() x 2 |
| 18 | -- | OK | getParent() x 2 |
| 19 | -- | OK | getParent() x 2 |
|_____|________________________|_________________________|______________________|
Then, the right condition should be:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// retrieve the two parents with "android.R.id.home"
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// retrieve the first parent with "android.R.id.home"
} else {
// retrieve the first parent with "R.id.home"
}
You can set the background of the home button to null
by using View.getParent
.
This is the layout that contains the home button, once inflated in ActionBarView
, is added to the up container layout which is what actually uses the actionBarItemBackground
attribute.
However, AppCompat
doesn't utilize an "up container" layout, as well as the normal ActionBar
up to API level 18 (Jelly Bean MR2).
So, to change the home button background you'd make one call to View.getParent
when using the support library or API levels 11-17's ActionBar
, but two when you're changing API level 18+'s.
View home;
final int version = Build.VERSION.SDK_INT;
if (version >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
// Normal action bar, post-"up container"
home = (View) findViewById(android.R.id.home).getParent().getParent();
} else if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Normal action bar, pre-"up container"
home = (View) findViewById(android.R.id.home).getParent();
} else {
// Support library action bar
home = (View) findViewById(R.id.home).getParent();
}
home.setBackgroundDrawable(null);
AppCompat
could be updated in the future to include to an "up container" layout, so bear in mind you may need to adjust your code later on to accommodate for that.
这篇关于如何禁用时的背景颜色HomeUp是pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!