如何将主题添加到应用程序?

时间:2016-02-13 07:35:25

标签: java android themes color-scheme

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

enter image description here enter image description here

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

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

2 个答案:

答案 0 :(得分:3)

您当然可以创建自己的自定义主题。但是您需要将任何默认主题用作父级。为此做了以下

首先在getText()

中定义颜色
final String[] message = new String[2];
final ImageView iv = (ImageView)findViewById(R.id.main_item2);
final TextView tv = (TextView)findViewById(R.id.storePostions);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
  public boolean onPreDraw() {
      iv.getViewTreeObserver().removeOnPreDrawListener(this);
      int finalHeight, finalWidth;
      finalHeight = iv.getMeasuredHeight();
      finalWidth = iv.getMeasuredWidth();
      message[0] = String.valueOf(finalHeight);
      message[1] = String.valueOf(finalWidth);
      return true;
    }
});

Toast.makeText(MainActivity.this, "finalHeight = " + message[0] + " ,finalWidth = " + message[1], Toast.LENGTH_LONG).show();
// show "finalHeight = NULL ,finalWidth =  NULL"

然后您需要在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>

在style.xml(v21)中,您需要使用以下代码

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>

最后因为我们使用了没有actiobar所以你需要在 <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>

并在activity_main中。 xml包含以下代码。

activity_main.xml

您可以在“appcompat”活动中设置支持操作栏,如下所示

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

部分链接

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

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

    enter image description here

答案 1 :(得分:0)

创建自定义应用主题

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:

Checkout The Link For more detail