android - 在android中动态改变昼夜模式

标签 android

我正在开发一个基于 Android 的 GPS 导航应用程序,有必要根据光传感器值为该应用程序实现自动日夜主题切换器。问题是我必须在不重新启动 Activity 或应用程序的情况下更改主题。

我试图通过遵循本教程 (http://sriramramani.wordpress.com/2012/12/06/runtime-theme-change/) 来实现这一点,其中自定义状态用于在白天和夜间模式之间切换。但它需要大量更改,因为我的应用程序有很多布局。此外,对于此实现,开发人员还必须为 future 的屏幕处理白天和夜间模式。

有什么简单有效的方法可以做到这一点吗?

最佳答案

ViewFlipper!这就是我在我的应用程序中使用的,允许用户选择白天或夜晚的主题。同样由于 java 的性质,它可以根据任何 bool 值进行更改(即,如果光线比 2 秒前的光线更暗)。也就是说,没有真正简单的方法可以自动切换所有布局,而无需为切换编码或在其他人的工作基础上进行构建。

方法如下:

我的应用程序有 1 个 Activity 、1 个布局和一个用于切换颜色的按钮。

  • MainActivity.java
  • activity_main.xml(包含按钮)

一个 ViewFlipper 作为每个布局的子元素放置,如下所示:(每个布局都需要白天和夜间模式)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ViewFlipper
    android:id="@+id/viewFlipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  </ViewFlipper>

</RelativeLayout>

然后我为每个布局制作白天彩色布局和夜间彩色布局

Day 和 Night 布局都必须是 ViewFlipper 的子级,所以如果你折叠这两个布局,它应该是

    • 日元素放这里
    • 夜间元素放在这里
  • /ViewFlipper>

麻烦来了:每个元素都必须有一个唯一的 ID,因为它们在同一个文档中,例如,如果你想在同一个屏幕上显示一个白天按钮和一个晚上按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ViewFlipper
  android:id="@+id/viewFlipper"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:id="@+id/DayLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="15dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profileDayBtn"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#fff"
        android:textColor="#000" />
</LinearLayout>
<LinearLayout
    android:id="@+id/NightLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/profileNightBtn"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="15dp"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#000"
    android:textColor="#fff" />
</LinearLayout>

</ViewFlipper>
</RelativeLayout>

这就是布局,现在要真正改变它:

在每个具有白天或夜间布局的 Activity 中添加此代码:(我将我的添加到每个 Activity 的 onCreate 但这不会用于基于时间的切换)

    setContentView(R.layout.activity_main);

    Button dayBtn = (Button) findViewById(R.id.profileDayBtn);
    Button nightBtn = (Button) findViewById(R.id.profileNightBtn);

    View.OnClickListener changeColorMode = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
          if (v == dayBtn) {
            vf.showNext();
          } else if (v == nightBtn) {
            vf.showPrevious();
          }
    };
    dayBtn.setOnClickListener(changeColorMode);
    nightBtn.setOnClickListener(changeColorMode);
    }

layout 被设置为 activity_main 但因为 activity_main 有一个 viewflipper,所以它将它设置为 viewflipper 内的 first root(索引 0)(这是我的日常布局),.showNext() 将显示下一个根元素(索引 1 我的夜间布局等等,如果我添加更多的话)

关闭,如果您需要显示特定布局而不是猜测 .showNext() 您可以调用

   viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(XXX));

其中 XXX 可以用布局的索引 (0, 1, ect) 替换,或者 XXX 可以用这样的特定布局 ID 替换 viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.DayLayout));

这就是我使用的,效果很好,因为它非常易于理解并且非常可定制!希望这有帮助

-斯蒂芬

关于android - 在android中动态改变昼夜模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731198/

相关文章:

android - 查看 fragment 中间的寻呼机选项卡

android - Android开发者控制台是否忽略uses-feature android :glEsVersion?

android - 如何更改 Android 的选项菜单位置

java - 为什么我的进度条不起作用?

android - 在每次触摸事件时旋转图像

android - 在哪里添加 Proguard 自定义规则?

java - Android 应用程序运行时出错

Android 按钮选择器背景色

android - jquery scrollLeft 不适用于 Android

android - 为 Android map 中的区域添加颜色