【问题标题】:Android Lollipop, how to change color for different UI elements (ProgressBar, Switch, TextInput)Android Lollipop,如何更改不同 UI 元素的颜色(ProgressBar、Switch、TextInput)
【发布时间】:2015-11-08 14:05:34
【问题描述】:

我有一个 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)。我需要一个使用这些全局设置的解决方案。

【问题讨论】:

    标签: android react-native


    【解决方案1】:

    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>
    

    示例:

    【讨论】:

    • 我不明白,我应该把颜色代码放在哪里?
    【解决方案2】:

    这样的一些代码:

    <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>
    

    【讨论】:

    猜你喜欢
    • 2010-12-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多