问题
I am testing out my app for API21 on Android! Unfortunately, my buttons seem weird - there is some kind of background color added. The background images did not change - they are completely transparent except for the border. (The different text and size is due to the screenshot).
Here you see the buttons before and since API 21: http://imgur.com/9EEcl0o,yVEnJkI#0
I already tried android:elevation="0dp" and android:background="@android:color/transparent". Anybody knows why my buttons change? Thank you very much!
layout.xml:
<Button android:id="@+id/sm_achievements_btn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.1"
android:background="@drawable/menu_btn_selector"
android:onClick="showAchievements"
android:text="@string/sm_achievements_btn"
android:padding="@dimen/padding_std"
android:layout_marginTop="@dimen/margin_small"
android:textColor="@color/button_text"
android:textSize="@dimen/text_xlarge"
style="@style/lbm_button"/>
menu_btn_selector.xml:
<item android:drawable="@drawable/menu_button"
android:state_pressed="false"/>
style.xml:
<style name="lbm_button" parent="android:Theme.Holo.Light">
<item name="android:textAllCaps">false</item>
</style>
回答1:
Material buttons have a default stateListAnimator that provides state-based elevation (e.g. 0dp when disabled, 1dp when enabled). You can clear it by setting android:stateListAnimator="@null" in your style or directly on the Button.
Here is what that would look like on your button XML:
<Button android:id="@+id/sm_achievements_btn"
...
android:textAllCaps="false"
android:stateListAnimator="@null" />
Also, you're using the wrong parent for your button style. You should never set a theme as the parent for a widget style. Regardless, here is what that should look like if you prefer that route:
<Button android:id="@+id/sm_achievements_btn"
...
style="@style/MyButtonStyle" />
<style name="MyButtonStyle" parent="android:Widget.Material.Light.Button">
<item name="android:textAllCaps">false</item>
<item name="android:stateListAnimator">@null</item>
</style>
来源:https://stackoverflow.com/questions/26473244/android-api-21-changes-my-custom-button-background