java - 如何向应用程序添加主题?

标签 java android themes color-scheme

我想在我的应用程序中添加不同的主题。我想更改导航栏颜色和 float 操作按钮的颜色。对于不同的主题,应该设置不同的颜色。我需要为不同的主题设置配色方案。

enter image description here enter image description here

喜欢这些图片。对于浅色主题浅色,对于深色主题深色。

我该怎么做?任何教程或建议请.. 谢谢。

最佳答案

您当然可以创建自己的自定义主题。但您需要使用任何默认主题作为父主题。为此,请执行以下操作

首先在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>

完成所有这些后,不要忘记将此主题添加到您的 list 中

    <?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.c中。 xml 将其包含在以下代码中。

<include
 android:id="@+id/toolbar"
 layout="@layout/toolbar" />

从您的“appcompat” Activity 中,您可以设置支持操作栏,如下所示

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

一些链接

  1. Material 调色板 -- https://www.materialpalette.com/

  2. 哪个颜色属性定义了下图中显示的部分

    enter image description here

关于java - 如何向应用程序添加主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377352/

相关文章:

java - 构建时出现 MojoExecutionException - Maven

java - Android Studio 中名称值对中的内容如何作为参数传递?

android - 如何在android中使用动画将 View 移动到屏幕中心

ruby-on-rails-3.1 - 如何将购买的 "theme"相关javascript、css 和image 文件组织成Rails 3.1 应用程序?

css - 2.3 Nopcommerce 主题更新到3.0了?

java - 当未使用或设置节点内定义的 Freemarker 变量时删除 XML 节点

java - 为什么对字节进行右移 (>>) 位运算会产生奇怪的结果?

java - 以相同的方法读取 SQLite 数据或以相同的方法从两个表中读取数据

android - 如何动态更改主题的文本颜色?

java - 如何创建 .jar 冷