【问题标题】:Change default design theme to customized color将默认设计主题更改为自定义颜色
【发布时间】:2014-09-14 04:19:32
【问题描述】:

我对 Android Studio 很陌生。作为初学者,我创建了一个简单的应用程序,仅用于测试目的并查看 Android Studio Material 主题的外观。我目前正在使用最新版本,即。 l 预览 - Studio 0.8.2 版本。

在这里,我刚刚创建了 textview、edittext、单选按钮和复选框。当我选择男性或女性时,它会出现天蓝色。我可以将天蓝色更改为绿色或黄色吗?

我不知道它叫什么名字。从图片中可以看到edittext、单选按钮和复选框的选中状态为天蓝色。当我尝试单击或选择任何内容时,这些复选框或单选按钮需要从默认颜色更改为其他颜色,例如绿色或黄色!

代码

在 res/values/styles.xml,

<resources>
<!-- Base application theme. -->
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Material">
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
</resources>

在 Android Studio Manifest.xml 中,

android:theme="@style/CustomTheme" >

还有没有其他方法可以从默认的天蓝色变为绿色或黄色或紫色?

有没有其他方法可以将默认的天蓝色主题更改为其他颜色,如粉红色或绿色?

我该怎么做?

【问题讨论】:

    标签: android material-design android-styles android-checkbox


    【解决方案1】:

    第一种方法:

    在 res/values-v21/styles.xml,

    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:colorAccent">@color/custom_theme_color</item>
        <item name="android:colorPrimaryDark">@color/custom_theme_color</item>
        <item name="android:colorPrimary">@color/custom_theme_color</item>
    </style>
    

    上面的代码很简单,运行良好。在 Android Studio 中,它可以从默认的天蓝色更改为任何其他颜色,如粉红色或绿色!

    第二种方法:

    在 xml 下创建 3 个文件,即。 res/xml 例如checked.xml、unchecked.xml和custom_checkbox.xml

    checked.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2px" android:color="#FF00FF" />
    <size android:height="20dp" android:width="20dp"/>
    </shape>
    

    unchecked.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
    </shape>
    

    custom_checkbox.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" 
          android:drawable="@xml/checked" /> 
    <item android:state_pressed="true"
          android:drawable="@xml/checked" /> 
    <item android:drawable="@xml/unchecked" /> 
    </selector>
    

    通过上述第二种方法,它(自定义文件)也可以根据我们的要求创建任何形状和颜色。甚至我们也可以使用第二种方法自定义小部件的样式、形状和主题。

    以上两种方法对我来说效果很好!

    【讨论】:

      【解决方案2】:

      从 v23 开始,您还可以使用 AppCompat 使用 android.support.v7.widget.AppCompatCheckBox 并像这样更改 CheckBox 样式:

      <android.support.v7.widget.AppCompatCheckBox
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:theme="@style/CheckBox"
          />
      

      style.xml 中的位置:

      <style name="CheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
          <item name="colorControlNormal">@color/checkbox_normal</item> <!-- border color -->
          <item name="colorControlActivated">@color/checkbox_activated</item> <!-- fill color -->
          <item name="colorControlHighlight">@color/checkbox_highlight</item> <!-- animation color -->
      </style>
      

      【讨论】:

        猜你喜欢
        • 2012-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-30
        • 2016-10-09
        相关资源
        最近更新 更多