2016-02-13 24 views
0

我想在我的應用程序中添加不同的主題。我想更改導航欄顏色和浮動操作按鈕的顏色。對於不同的主題,應設置不同的顏色。我需要爲不同主題設置配色方案。如何將主題添加到應用程序?

enter image description here enter image description here

像這些圖像。用於淺色主題淺色和深色主題深色。

我該怎麼做?任何教程或建議請.. 謝謝。

回答

3

當然,您可以創建自己的自定義主題。但是您需要使用任何默認主題作爲父項。這個做以下

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> 

畢竟這些都不是forg等這一主題添加到您的清單

<?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> 

,最後因爲我們沒有使用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" /> 

從你的 '程序兼容性' 活動,你可以設置支持動作條,如下

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

一些鏈接

  1. 材料調色板 - https://www.materialpalette.com/

  2. 哪種顏色屬性定義顯示在下面的圖像哪一部分

    enter image description here

+0

不錯的答案。讓我try..Will回來到u .. @ Zahan Safallwa – Sid

0
Creating a Custom App Theme 
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> 

Add a resources node to styles.xml and define a style node with the name of your custom theme. For example, here is a styles.xml file that defines MyCustomTheme (derived from the built-in Theme.Material.Light theme style): 

<?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: 

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