在Android的动态变化白天夜间模式 [英] Changing day night mode dynamically in android

查看:154
本文介绍了在Android的动态变化白天夜间模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个基于Android的GPS导航应用程序,并实现了基于光传感器值应用自动日夜切换主题,它是必要的。问题是我必须要改变主题无需重新启动活动或应用程序。

I am working on an Android based GPS Navigation application and it is necessary to implement automatic day and night theme switcher for the application based on the light sensor values. The problem is I have to change the theme without restarting the Activity or the application.

我试图按照本教程(的 HTTP://sriramramani.word$p$pss.com/2012/12/06/runtime-theme-change/ ),其中自定义状态用于白天和夜间模式之间切换。但是,它需要很多的变化我的应用程序有很多布局。也与此实现开发商必须照顾日夜模式为未来的画面。

I tried to achieve this by following this tutorial (http://sriramramani.wordpress.com/2012/12/06/runtime-theme-change/) where custom states are used to change between day and night mode. But it requires lot of changes as my application has many layouts. Also with this implementation developers have to take care of Day and Night modes for future screens.

有没有什么简单有效的方式来实现这一目标?

Is there any simple and effective way to achieve this?

推荐答案

ViewFlipper! 这是我在我的应用程序使用,以允许用户的白天或夜晚的主题的选项。 (即如果光线较暗比它2秒去)也因为Java的性质可以基于任何布尔值被改变。话虽这么说,没有真正简单的方法来自动使所有的布局切换无需编码为别人工作中的交换机或建筑物。

ViewFlipper! This is what I use in my applications to allow the user the option of day or night themes. Also because of the nature of java it can be changed based on any boolean value (i.e. if light is darker than it was 2 seconds go). That being said, there is no real easy way to automatically make all your layouts switch without coding for the switch or building on someone else's work.

下面的方式:

我有了1​​活动,1布局,一键切换颜色的应用程序。

I have an application that has 1 activity, 1 layout, and a button to switch the colors.


  • MainActivity.java

  • activity_main.xml中(包含按钮)

一个ViewFlipper被作为每个布局子元素,像这样:(每个需要在白天和夜间模式布局)

a ViewFlipper is placed as every layouts child element like so: (every layout that needs both day and night mode)

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

然后为每个布局我做日间有色布局及夜间彩色布局

Then for each layout I make Day Time colored layout and Night Time Colored Layout

白天和夜间的布局必须是ViewFlipper的孩子,所以如果你崩溃两种布局应该是

Both Day and Night layouts must be children of the ViewFlipper so if you collapse both layouts it should be


  • &LT; ViewFlipper>

    • &LT;的RelativeLayout> 日期元素何去何从&LT; /的RelativeLayout>

    • &LT;的RelativeLayout> 夜间元素去这里&LT; /的RelativeLayout>

    • < ViewFlipper>
      • < RelativeLayout> Day elements go in here < /RelativeLayout>
      • < RelativeLayout> Night Elements go in here < /RelativeLayout>

      这里谈到的麻烦:每个元素都必须有一个唯一的ID,因为它们是在同一文件中,如果你想在同一画面例如在天按钮和夜间按钮

      Here comes the hassle: Each element must have a unique id because they are in the same document, if you want a day button and night button on the same screen for example.

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

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

      That's it for the layouts, now to actually change it:

      这有白天或夜晚布局添加此code每一个活动里面(我加了我的每一个活动的onCreate,但是这不会对交换机基于时间做)

      inside every activity that has a Day or Night Layout add this code: (I added mine to the onCreate of every activity but this wont do for a switch based on time)

          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);
          }
      

      布局设置为activity_main但由于activity_main有viewflipper,但将其设置为viewflipper在第一根(索引0)(这是我一天的布局),.showNext()将显示接下来的根元素(索引1我一夜的布局等等,如果我加入了更多)

      layout was set to activity_main but because activity_main has a viewflipper, it set it to the first root (index 0) inside the viewflipper (which was my day layout), .showNext() will show the next root element (index 1 my night layout and so on if I added more)

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

      to close, if you need a specific layout shown instead of guessing with .showNext() you can call

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

      其中XXX可以与布局或(XXX)的索引(0,1,等)来代替可与这样的特定布局ID来代替
      <$c$c>viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.DayLayout));

      这是我用它的伟大工程,因为它非常简单易懂,很可定制!希望这有助于

      This is what i use and it works great because its very simple to understand and is very customizable! Hope this helps

      -Stephen

      这篇关于在Android的动态变化白天夜间模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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