如何将主题添加到应用程序?
How to add themes to application?
我想在我的应用程序中添加不同的主题。我想更改导航栏颜色和浮动操作按钮的颜色。对于不同的主题,应该设置不同的颜色。我需要为不同的主题设置配色方案。
喜欢这些图片。浅色主题为浅色,深色主题为深色。
我该怎么做?请任何教程或建议..
谢谢。
您当然可以创建自己的自定义主题。但是您需要使用任何默认主题作为父主题。为此,请执行以下操作
首先定义res->vales->color.xml
中的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#009999</color>
<color name="colorPrimaryDark">#006666</color>
<color name="textColorPrimary">#FFFFFF</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="colorAccent">#006666</color>
</resources>
然后你需要像下面这样定义你的主题 res->values->styles.xml
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="MyTheme.Base"></style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
在style.xml(v21)中你需要使用下面的代码
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
完成所有这些后,别忘了将此主题添加到您的清单中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxx.xx.xx.x"> //your pcakcage
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<activity
android:name=".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>
</manifest>
最后,由于我们使用了 No actiobar,因此您需要在 activity_main.xml
中包含工具栏。让工具栏像下面这样
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
并在 activity_main。 xml 将其包含在以下代码中。
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
并且从您的 'appcompat' activity 您可以将支持操作栏设置如下
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
部分链接
Material 调色板 -- https://www.materialpalette.com/
哪种颜色 属性 定义下图中显示的部分
创建自定义应用主题
colors.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<color name="my_blue">#3498DB</color>
<color name="my_green">#77D065</color>
<color name="my_purple">#B455B6</color>
<color name="my_gray">#738182</color>
</resources>
将资源节点添加到 styles.xml 并使用自定义主题的名称定义样式节点。例如,这里有一个 styles.xml 文件,它定义了 MyCustomTheme(派生自内置的 Theme.Material.Light 主题样式):
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<!-- Inherit from the light Material Theme -->
<style name="MyCustomTheme" parent="android:Theme.Material.Light">
<!-- Customizations go here -->
</style>
</resources>
With these changes in place, an app that uses MyCustomTheme will display an app bar color in my_blue and UI controls in my_purple, but use the Theme.Material.Light color scheme everywhere else:
我想在我的应用程序中添加不同的主题。我想更改导航栏颜色和浮动操作按钮的颜色。对于不同的主题,应该设置不同的颜色。我需要为不同的主题设置配色方案。
喜欢这些图片。浅色主题为浅色,深色主题为深色。
我该怎么做?请任何教程或建议.. 谢谢。
您当然可以创建自己的自定义主题。但是您需要使用任何默认主题作为父主题。为此,请执行以下操作
首先定义res->vales->color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#009999</color>
<color name="colorPrimaryDark">#006666</color>
<color name="textColorPrimary">#FFFFFF</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="colorAccent">#006666</color>
</resources>
然后你需要像下面这样定义你的主题 res->values->styles.xml
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="MyTheme.Base"></style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
在style.xml(v21)中你需要使用下面的代码
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
完成所有这些后,别忘了将此主题添加到您的清单中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.xxx.xx.xx.x"> //your pcakcage
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<activity
android:name=".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>
</manifest>
最后,由于我们使用了 No actiobar,因此您需要在 activity_main.xml
中包含工具栏。让工具栏像下面这样
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
并在 activity_main。 xml 将其包含在以下代码中。
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
并且从您的 'appcompat' activity 您可以将支持操作栏设置如下
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
部分链接
Material 调色板 -- https://www.materialpalette.com/
哪种颜色 属性 定义下图中显示的部分
创建自定义应用主题
colors.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<color name="my_blue">#3498DB</color>
<color name="my_green">#77D065</color>
<color name="my_purple">#B455B6</color>
<color name="my_gray">#738182</color>
</resources>
将资源节点添加到 styles.xml 并使用自定义主题的名称定义样式节点。例如,这里有一个 styles.xml 文件,它定义了 MyCustomTheme(派生自内置的 Theme.Material.Light 主题样式):
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<!-- Inherit from the light Material Theme -->
<style name="MyCustomTheme" parent="android:Theme.Material.Light">
<!-- Customizations go here -->
</style>
</resources>
With these changes in place, an app that uses MyCustomTheme will display an app bar color in my_blue and UI controls in my_purple, but use the Theme.Material.Light color scheme everywhere else: