如何设置日夜主题android应用 [英] how to set day and night theme in android application

查看:114
本文介绍了如何设置日夜主题android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建Android应用程序,我有使用列表prefances我已经设置白天模式主题模式和夜间模式主题

i have create an android application i have use list prefances i have set day mode theme and night mode theme

<style name="Theme.FullScreen" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen"></style>

<style name="PreferencesTheme" parent="android:Theme.Light">
    <item name="android:background">#FFEAEAEA</item>
</style>

<style name="PreferencesTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@drawable/ic_icon_settings</item>
</style>

这是我的styl.xml文件,请帮助我如何申请白天模式,夜晚模式,以及如何设置styl.xml filr

this is my styl.xml file please help me how apply day mode and night mode and how set styl.xml filr

推荐答案

您应该看的 HoneycombGallery 的例子从Android 3.2 API13示例应用程序。

You should look at the HoneycombGallery example from the Android 3.2 API13 sample applications.

他们的方式,他们已经实现了这一点:

They way they have implemented this is:

声明两个主题在你的 styles.xml

<style name="ActionBar" parent="@android:style/Widget.Holo.ActionBar" />

<style name="ActionBar.Light" parent="@style/ActionBar">
    <item name="android:background">@color/actionbar_background_light</item>
</style>

<style name="ActionBar.Dark" parent="@style/ActionBar">
    <item name="android:background">@color/actionbar_background_dark</item>
</style>

<style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar.Light</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="listDragShadowBackground">@android:color/background_light</item>
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_light</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_light</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_light</item>
</style>

<style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
    <item name="android:actionBarStyle">@style/ActionBar.Dark</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="listDragShadowBackground">@android:color/background_dark</item>
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_dark</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_dark</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_dark</item>
</style>

菜单切换模式之间:

这是 main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/toggleTheme"
        android:title="Day/Night"
        android:showAsAction="never" />
</menu>

MainActivity 类,他们之间切换的的夜间的模式:

And the MainActivity class where they toggle between the Day and Night modes:

注:我已经去掉了其他的东西,没有必要为这个问题的背景下

NOTE: I have stripped out the other stuff that is not necessary for the context of this question.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.toggleTheme:
        if (mThemeId == R.style.AppTheme_Dark) {
            mThemeId = R.style.AppTheme_Light;
        } else {
            mThemeId = R.style.AppTheme_Dark;
        }
        this.recreate();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

在模式之间切换所需的全局变量:

The Global Variable necessary for switching between the modes:

private int mThemeId = -1;

和用于保存最后选定的模式的,在OnCreate():

And for saving the last selected mode, in the onCreate():

if(savedInstanceState != null && savedInstanceState.getInt("theme", -1) != -1) {
    mThemeId = savedInstanceState.getInt("theme");
    this.setTheme(mThemeId);
}

但不要从SDK管理器下载样本,看看实际的(整个)功能。

But do download the samples from the SDK Manager and see the actual (entire) functioning.

这篇关于如何设置日夜主题android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆