2015-11-08 76 views
0

我有一个ProgressBar,Switch和TextInput,我需要能够为它们中的每一个指定不同的颜色。Android棒棒糖,如何更改不同UI元素的颜色(ProgressBar,Switch,TextInput)

当我这样做,它会改变所有3项的颜色。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorAccent">#e8c4c7</item> 
</style> 

我正在寻找一个只改变我想要的UI元素的颜色的命令。是这样的:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="progressBar">#aaaaaa</item> 
    <item name="switch">#bbbbbb</item> 
    <item name="textInput">#cccccc</item> 
</style> 

不幸的是,我无法直接改变每个元素的样式(使用ReactNative原因IM)。我需要一个使用这些全局设置的解决方案。

回答

0

一些这样的代码:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:progressBarStyle">@style/progress</item> 
    <item name="android:switchStyle">@style/swith</item> 
    <item name="android:textAppearance">@style/text</item> 

</style> 

<style name="progress" parent="Widget.AppCompat.ProgressBar.Horizontal"> 
    <item name="android:background"></item> 
</style> 

<style name="swith" parent="Widget.AppCompat.CompoundButton.Switch"></style> 
<style name="text" parent="TextAppearance.AppCompat"></style> 
+0

我尽我所能改变的圆形进度条的颜色,但没有运气。 https://gist.github.com/han4wluc/be5422c91bcfdde8fc8a – han4wluc

3

style.xml:

<resources> 
    <!-- inherit from the material theme --> 
    <style name="AppTheme" parent="android:Theme.Material"> 
    <!-- Main theme colors --> 
    <!-- your app branding color for the app bar, also for button color--> 
    <item name="android:colorPrimary">@color/primary</item> 
    <!-- darker variant for the status bar and contextual app bars --> 
    <item name="android:colorPrimaryDark">@color/primary_dark</item> 
    <!-- theme UI controls like checkboxes and text fields --> 
    <item name="android:colorAccent">@color/accent</item> 
    <!--progress bar theme--> 
    <item name="android:progressBarStyle">@style/myprogress</item> 
    <!--switch button theme--> 
    <item name="android:switchStyle">@style/myswith</item> 
    </style> 
</resources> 

<!--progress bar style,--> 
<style name="myprogress" parent="Widget.AppCompat.ProgressBar.Horizontal"> 
     <item name="android:background">@color/white_color</item> 
</style> 

<style name="myswitch" parent="Widget.AppCompat.CompoundButton.Switch"></style> 

的Manifest.xml:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".activity.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

样品:enter image description here

+1

我不明白,我应该在哪里把颜色代码? – stkvtflw

+0

@stkvtflw,将颜色代码放在colors.xml中。 – Androider