安卓按钮半透明的果冻豆,但不是棒棒糖



我有最新的Android Studio和SDK(Compile SDK 21,Build Tools 21.1.2,appcompat v7 21.0.3),并使用空白活动模板制作了一个全新的向导应用程序。

然后我只更改XML:根相对布局的背景(android:background="#00FF00"),并添加一个按钮(使用图形编辑器,不更改按钮)。

这在棒棒糖设备上运行良好。在软糖(4.2.2)设备或模拟器上,按钮呈现灰色,并带有绿色触摸。

这可能是一个主题(theme.AppCompat.LightDarkActionBar)的主题,但它在棒棒糖和果冻豆设备上的渲染是否应该相同?

我看到了关于手动更改按钮背景的问题(例如,如何使按钮不透明),这是一个兼容性问题。

以下是完整的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#00FF00">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="26dp"
    android:layout_marginStart="26dp"
    android:layout_marginTop="55dp" />
 </RelativeLayout>

AppCompat当前不支持按钮小部件,因此Holo主题(API级别小于21)上的按钮呈现方式与Material主题(API级别21+)上的不同。您需要定义这样的按钮样式:

<style name="MyButtonStyle" parent="Base.MyButtonStyle"/>

然后在res/values文件夹中定义Base.MyButtonStyle

<style name="Base.MyButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:background">@drawable/btn_default</item>
</style>

btn_default绘图将指向按钮的自定义选择器。

在res/values-v21中,您定义Base.MyButtonStyle如下:

<style name="Base.MyButtonStyle" parent="android:Widget.Material.Button"/>

如果您想更改21+按钮的正常或按下的颜色,您可以在主题中分别设置colorButtonNormal和colorControlHighlight。

如果您没有指定style或将android:background="yourBackground"添加到按钮的xml中,Android将使用您的应用程序运行的Android版本的默认按钮样式来呈现您的按钮。

例如,如果你在按钮上添加android:background="#FF0000",那么不同的android版本就不会有太大的差异了。

最新更新