【问题标题】:How to add themes to application?如何将主题添加到应用程序?
【发布时间】:2016-05-24 11:21:43
【问题描述】:

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

喜欢这些图片。用于浅色主题浅色和深色主题深色。

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

【问题讨论】:

    标签: java android themes color-scheme


    【解决方案1】:

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

    首先在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-&gt;values-&gt;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”活动中设置支持操作栏,如下所示

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

    一些链接

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

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

    【讨论】:

    • 不错的答案..让我试试..会回复你..@Zahan Safallwa
    【解决方案2】:

    创建自定义应用主题

    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 并使用自定义主题的名称定义样式节点。例如,这里是一个定义 MyCustomTheme 的 styles.xml 文件(源自内置的 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:

    Checkout The Link For more detail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2017-10-01
      • 2013-03-09
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多