带有支持库的 Pre-Lollipop 上的无边框按钮

Borderless Button on Pre-Lollipop with Support Library

我正在使用支持库 (23.0.1) 制作无边框平面按钮。它在 Lollipop 上正常工作。但是,当我按下按钮时,在棒棒糖之前,它的颜色会变为 colorButtonNormal 颜色,就像它是普通按钮一样。

我不这么认为,这是正常行为,聚焦颜色应该像 Lollipop 上那样是灰色。

这是 Lollipop 和 Pre-lollipop 的屏幕截图。

Lollipop 上的第一个正常行为: Lollipop 上正常状态下的无边框按钮和聚焦状态

Pre-Lollipop 上的不正常行为(想要的颜色像上面一样是灰色,但它不是): 正常状态下的无边框按钮和 Pre-lollipop 上的聚焦状态

主题

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
//other stuff 
        <item name="colorButtonNormal">@color/orangeColor</item>
        <item name="buttonBarButtonStyle">@style/BorderlessButtonStyle</item>
</style>

<style name="BorderlessButtonStyle" parent="Widget.AppCompat.Button.Borderless">
        <item name="android:textColor">@color/blueTextColor</item>
</style>

现在布局中的按钮:

<Button
            android:id="@+id/btnForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/forgot_your_password"
            style="?attr/buttonBarButtonStyle"
            />

使用 AppCompat 主题和样式编写它而无需制作单独的 Drawable 的任何方法。

您正在使用 android 实现的 "buttonBarButtonStyle" 样式,因为您是通过 ?atr 调用它 - 请改用 style="@style/BorderlessButtonStyle

编辑:保持 xml 不变,但您可以将其更改为您想要的行为,如下所示:

AppCompatButton button = (AppCompatButton) findViewById(R.id.btnForgotPassword);

ColorStateList colorStateList = new ColorStateList(new int[][] {{0}}, new int[] {0xFF969696});  
    //969696 is your wanted grey color, just change it
button.setSupportBackgroundTintList(colorStateList);

为什么你会担心一些事情就随它去自由

<Button
   android:id="@+id/btnForgotPassword"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/forgot_your_password"
   android:background="@drawable/abc_btn_borderless_material"
/>

现在对 api 事情粗心大意

Borderless Button 适用于 Post 和带有支持库的 Pre Lollipop 版本,但它们的 onPressed 颜色之间存在细微差别。

Pre-Lollipop:默认情况下,onPressed 颜色与使用 colorButtonNormal 设置的默认按钮颜色相同。

棒棒糖:默认onPressed颜色为浅灰色,比较理想。

你可以像这样制作无边框按钮:

<Button  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name"
    style="@style/Widget.AppCompat.Button.Borderless"/>

现在,如果您想在所有版本上使用相同的 onPressed 颜色,那么您可以在新主题中设置 colorControlHighlight,然后在 Button 上设置该主题。

<Button  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:theme="@style/BorderlessButton"
        style="@style/Widget.AppCompat.Button.Borderless"/>

和你风格的主题:

<style name="BorderlessButton" parent="Theme.AppCompat.Light">
      <item name="colorControlHighlight">YOUR COLOR</item>
</style>

已更新:您可以对 View 使用 android:theme 属性,因为 Android 5.0 Lollipop 和 AppCompat v22.1.0(及更高版本) .

style="?borderlessButtonStyle" 添加到 Button 对我来说效果很好。