繁体   English   中英

Android-单击时如何更改所有按钮的颜色

[英]Android - How to change the color of all button when it being clicked

我想知道的是,是否有办法在整个活动中更改任何点击颜色。

我已经设置了最丰富的下一个活动-

                <activity android:name="com.example.test.MainActivity" 
                android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" 
                 android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" >
            </action>

            <category android:name="android.intent.category.NORMAL" >
            </category>
        </intent-filter>
    </activity>

如您所见,我正在为活动使用下一个主题-

  android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" 

这会使任何点击颜色变为蓝色,这意味着当按下按钮或其他任何按钮时,它会变成蓝色。

现在我想知道有什么方法可以将蓝色更改为另一种颜色,现在可以通过更改我要使用的每个视图项目的背景。

谢谢你的帮助

文件名:/res/drawable/button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@color/color1"/>
<item android:state_focused="true" android:drawable="@color/color1"/>
<item android:state_pressed="true" android:drawable="@color/color1"/>
<item android:drawable="@color/color1"/> 

将此文件放入您的Button样式中,如下所示:

在资源值文件夹中创建新样式,如下例,并更改应用程序属性android:theme="@style/CustomActionBarTheme"

 <!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@style/Theme.Base.AppCompat.Light.DarkActionBar>

    <item name="android:buttonStyle">@style/srp_button</item>

</style>
<style name="srp_button" parent="@android:style/Widget.Button">
<itemname="android:background">@drawable/button_color</item> <!-- Here isspecifiedtext color -->
</style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM