【问题标题】:how to set day Mode and Night Mode如何设置日间模式和夜间模式
【发布时间】:2016-02-15 20:43:16
【问题描述】:

主题.class

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/padding_medium"
            tools:context=".MainActivity" />
    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginRight="59dp"
            android:layout_marginTop="41dp"
            android:text="Button" />
</RelativeLayout>

我想在 android 中的 Button click 上设置日夜主题。请告诉我如何做到这一点。如何创造风格和所有。我是安卓新手。我试了很多都做不到。

【问题讨论】:

  • @vinil ll 请帮我设置日夜模式主题?
  • @codo 你帮我设置日夜模式主题?
  • 欢迎来到 Stack Overflow。下次请使用正确的大写/小写字母和标点符号。恐怕我不明白你的问题。您想为您的应用和/或手机打开和关闭夜间模式吗?还是想为日夜模式提供不同的 UI 样式?或者你到底想达到什么目标,你在哪里卡住了?
  • 我想在应用中设置夜间模式和白天模式
  • 我想设置白天模式和夜间模式,以便用户在白天模式下点击按钮,然后它应该改变请帮助我,请将其作为虚拟示例发送

标签: android


【解决方案1】:

加你resvalues-night并定义colors.xml。在ApplicationActivitysuper.onCreate() 之后定义
UiModeManager umm = (UiModeManager){#context}.getSystemService(Context.UI_MODE_SERVICE); umm.setNightMode(UiModeManager.MODE_NIGHT_YES); //MODE_NIGHT_YES|MODE_NIGHT_NO|MODE_NIGHT_AUTO
官方文档 - UiModeManager
此代码在 API 23 中有效,在 19 中可能没有

【讨论】:

    【解决方案2】:

    查看此答案中的解决方案:how to set day and night theme in android application

    我相信 SDK 的示例中有一个完整的功能示例,从 API 11 到 API 16(我还没有下载 API 17)。

    我已经使用了链接答案中提供的解决方案并且它有效。

    希望这对你也有帮助。

    【讨论】:

    • 我已经尝试过,但是当我在我的应用程序中实现它时会出现错误
    • public static void onActivityCreateSetTheme(Activity activity) { switch (sTheme) { default: case THEME_WHITE: activity.setTheme(R.style.Theme_White);休息;案例 THEME_BLUE:activity.setTheme(R.style.Theme_Blue);休息;此 Theme_White 中的 } } 无法解析或不是字段
    • 如何解决这个错误@Codo
    • 我看不出这段代码 sn-p 与夜间模式有何关系。它缺少很多上下文。请打开一个新问题,添加所有相关代码并使用{ } 按钮对其进行格式化。
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2018-05-09
    • 2018-11-22
    相关资源
    最近更新 更多