如何改变主题的触摸高亮颜色?

我想要替代或者覆盖主题的默认触摸高亮颜色,目前,使用actionBarItemBackground已经成功地更改了主题栏按钮的高亮颜色。但是,我在寻找一种应用程序范围变化的幅度,保证在按下按钮,动作条,抽屉或菜单时,会显示我所设置的默认颜色。我尝试设置colorPressedHighlight.和colorFocusedHighlight的属性,但是不起作用。我也想要改变,滚动条滚动到底时的提示颜色,以及想要继续下拉的渐变色。该怎么解决呢?
请输入图片描述

原问题:Override theme color for touch highlight, scroll hinting

阅读 5.9k
1 个回答

答:Manu Zi
你可以下载这个软件,然后装到应用上,这样就可以改变主题风格了。http://android-holo-colors.com/

答:Kevin van Mierlo
我相信,下述方法会对你有帮助,它主要是通过设置应用中的可选择项目的背景颜色,完成触摸高亮颜色的改变:

<style name="Theme.Holo">
    <!-- bunch of things -->
    <item name="android:selectableItemBackground">@android:drawable/item_background_holo_dark</item>
    <!-- bunch of things -->
</style>

它的drawable XML是:

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_dark" />
<item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_disabled_holo_dark" />
<item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
<item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_dark" />
<item android:state_focused="true"                                                             android:drawable="@drawable/list_focused_holo" />
<item                                                                                          android:drawable="@color/transparent" />

答:KBusc
你是要在应用的drawable文件夹中添加一个custom_color.xml文件,因此你可以按照自己的需求将其命名:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="#004F81"
            android:endColor="#004F81"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="#004F81" />
        <corners
            android:radius="3dp" />          
    </shape>
</item>
<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="#004F81"
            android:startColor="#004F81"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="#004F81" />
        <corners
            android:radius="3dp" />         
    </shape>
</item>
<item>        
    <shape>
        <gradient
            android:endColor="#43AFE8"
            android:startColor="#43AFE8"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#43AFE8" />
        <corners
            android:radius="6dp" />
        <padding
            android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp" />
    </shape>
</item>
</selector>

然后在所有的layout xml文件中,再设置每个元素的背景即可:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_color"
        android:textColor="#FFFFFF"
        android:text="Button" />

实践证明,这种代码工作效果不错。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题