【问题标题】:ImageButton background color with appcompat带有 appcompat 的 ImageButton 背景颜色
【发布时间】:2016-08-29 17:31:13
【问题描述】:

我无法从 AppCompatImageButton 获得良好的背景,只是想尝试比较这两种布局:

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/imageButtonI"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/icolor"
        android:tint="@color/accent"
        app:srcCompat="@drawable/magnify"/>

    <ImageButton
        android:id="@+id/imageButtonS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/scolor"
        android:tint="@color/accent"
        android:src="@drawable/magnify"/>

和样式文件:

<style name="AppBaseTheme" parent="android:Theme.Material">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primaryDark</item>
    <item name="android:colorAccent">@color/accent</item>
</style>

从这张图片可以看出,ImageButton 的背景是“正常的”,而 appcompat 中没有任何背景。如何使用AppCompatImageButton 获得“正常”背景?

【问题讨论】:

    标签: android android-appcompat


    【解决方案1】:

    设置按钮样式的一个好方法是使用样式@style/Widget.AppCompat.Button.Colored

    Widget.AppCompat.Button.Colored 样式扩展了 Widget.AppCompat.Button 样式并自动应用您在应用主题中选择的强调色。

    <Button
        style="@style/Widget.AppCompat.Button.Colored"
     />
    

    要自定义背景颜色而不更改主主题中的强调色,您可以使用android:theme 属性并扩展ThemeOverlay 主题为您的按钮创建自定义主题。

     <Button  
         style="@style/Widget.AppCompat.Button.Colored"  
         android:theme="@style/MyButtonTheme"/> 
    

    定义以下主题:

     <!-- res/values/themes.xml -->
      <style name="MyButtonTheme" parent="ThemeOverlay.AppCompat.Light"> 
          <item name="colorAccent">@color/my_color</item> 
     </style>
    

    【讨论】:

    • @greywolf82 我终于明白是什么原因了!您必须从 AppCompatActivity 扩展您的活动。
    • pre 21背景是白色的。请问如何解决?
    【解决方案2】:

    我不确定您使用的是哪个版本的支持库,但在此处报告的库中存在错误之前,

    https://code.google.com/p/android/issues/detail?id=78428

    但是他们已经在最新的更新中解决了,

    是的,它现在似乎与引入 AppCompat v22.1.0 中的 android.support.v7.widget.AppCompatButton,颜色 可以使用“colorButtonNormal”在整体主题级别进行控制。

    http://android-developers.blogspot.com/2015/04/android-support-library-221.html http://chris.banes.me/2015/04/22/support-libraries-v22-1-0/

    主题

    <item name="colorButtonNormal">@color/button_color</item>
    

    对于版本 21

    <item name="android:colorButtonNormal">@color/button_color</item>
    

    希望这会对你有所帮助。

    谢谢

    【讨论】:

    • 尝试在你的主题中使用上述
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多