版本5上带有getBackground()。setAlpha的按钮-棒棒糖无法正常工作 [英] Button with getBackground().setAlpha on version 5 - lollipop isn't working correctly
问题描述
我有这段代码,并且适用于API 14之后的每个版本,但是在Android 5.0(Lollipop)上无法正常工作。
I have this code and works for every version since API 14 but on Android 5.0 (Lollipop) isn't working correctly.
下面是我想要的方式出现的按钮。
Below is the way how I want the buttons to appear.
单击button1
click of button1
buttonArrivals.getBackground().setAlpha(180);
buttonDepartures.getBackground().setAlpha(255);
单击按钮2
click of button2
buttonArrivals.getBackground().setAlpha(255);
buttonDepartures.getBackground().setAlpha(180);
在棒棒糖版本中,按钮显示相同的Alpha,但我从未设置相同的Alpha。我只是使用上面的代码。
On the Lollipop version, the buttons appear with the same Alpha but I never set the same alpha. I just use the code above.
更新2014年11月24日
这是按钮的XML(AutoResizeButton扩展按钮)
Here is the XML of the buttons (AutoResizeButton extends Button)
br.com.timo.gru.util.AutoResizeButton
android:id="@+id/buttonArrivals"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00abbd"
android:drawableLeft="@drawable/icon_aviao_desemb"
android:drawablePadding="-5dp"
android:drawableStart="@drawable/icon_aviao_desemb"
android:gravity="center"
android:paddingEnd="0dp"
android:paddingLeft="2dp"
android:paddingRight="0dp"
android:text="@string/chegadas"
android:textColor="@android:color/white"
br.com.timo.gru.util.AutoResizeButton
android:id="@+id/buttonPartidas"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00abbd"
android:drawableLeft="@drawable/icon_aviao_partida"
android:drawablePadding="-5dp"
android:drawableStart="@drawable/icon_aviao_partida"
android:ellipsize="end"
android:gravity="center"
android:text="@string/partidas"
android:textColor="@android:color/white"
推荐答案
内部ColorState(由ColorDrawable使用)在这两个按钮之间共享(优化),因此只要您在一个按钮的背景上更改Alpha,其他按钮也将获得此更改。
您可以尝试在更改背景可绘制对象的alpha之前对其进行突变:
Internally ColorState (used by ColorDrawable) is shared between these 2 buttons (optimization), so whenever you change alpha on one button's background - other button will get this change as well. You can try to to mutate background drawable before changing its alpha:
buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);
您也可以阅读Romain Guy关于发生这种情况的很好解释: http://curious-creature.org/2009/05/02/drawable-mutations
You can also read good explanation from Romain Guy on why this is happening: http://curious-creature.org/2009/05/02/drawable-mutations
但是,您似乎尝试实现一些可以使用Android选择器轻松实现的功能。您可以为每种按钮状态(在您选择的情况下/未选择的情况下)指定不同的颜色,因此在代码中,您只需要更新状态:
However, it looks like you try to implement something which is easily achievable with Android selectors. You can specify different color for each button state (in your case selected/not selected), so in your code you just need to update state:
buttonArrivals.setSelected(true);
buttonDepartures.setSelected(false);
选择器如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ff00abbd"
android:state_selected="true" >
</item>
<item android:color="#b400abbd"
android:state_selected="false">
</item>
</selector>
这篇关于版本5上带有getBackground()。setAlpha的按钮-棒棒糖无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!