如何使用switchStyle属性将自定义样式应用于所有开关

时间:2019-01-06 09:29:20

标签: android android-theme android-styles android-switch

我知道我们可以简单地使用theme属性来实现此目的,并在视图中使用<style name="MySwitch"> <item name="colorControlNormal">@color/grey</item> <item name="colorControlActiviated">@color/primaryColor</item> </style> <Switch ... app:theme="@style/MySwitch"/> 属性来应用样式。例如,

theme

但是在这种情况下,我们需要将<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/primaryColor</item> <item name="colorPrimaryDark">@color/primaryDarkColor</item> <item name="colorAccent">@color/secondaryColor</item> <item name="switchStyle">@style/MySwitch</item> </style> 属性应用于活动或应用程序中的所有开关。相反,我想将其应用于整个应用程序主题。因此,我尝试通过这些方法实现相同的目的,但根本没有效果。

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="buttonTint">@color/primaryColor</item>
</style>

方法1:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryColor" android:state_checked="true" />
    <item android:color="@android:color/darker_gray" />
</selector>

switch_thumb:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryLightColor" android:state_checked="true" />
    <item android:color="@android:color/darker_gray" />
</selector>

switch_track:

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="thumbTint">@drawable/switch_thumb</item>
    <item name="trackTint">@drawable/switch_track</item>
</style>

方法2:

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="thumb">@drawable/switch_thumb</item>
    <item name="track">@drawable/switch_track</item>
</style>

方法3:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mato</groupId>
  <artifactId>lchain</artifactId>
  <version>0.0.3-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>lchain</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.web3j</groupId>
        <artifactId>core</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
   </dependency>
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.5</version>
   </dependency>
  </dependencies>

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                            <manifest>
                                <mainClass>
                                    com.mato.lchain.App
                                </mainClass>
                            </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我试图找到其他解决方案,但它们不包含将样式应用于我想要的整个主题的信息。

0 个答案:

没有答案