动态更改活动背景

时间:2016-03-31 17:26:45

标签: android android-studio

我正在开发一个天气应用程序,我希望改变我的活动背景,因为天气变化像白天或白天到黑夜。我似乎无法找到答案。 一点点帮助将深受赞赏。

2 个答案:

答案 0 :(得分:1)

试试这个:

print df2
                lat_mean            date_min            date_max  date_count  \
ser_no CTRY_NM                                                                 
1      a             1.5 2016-02-24 00:00:00 2016-02-24 10:00:00           2   
       b             3.0 2016-02-24 20:00:00 2016-02-24 20:00:00           1   
2      a             6.0 2016-02-26 02:00:00 2016-02-26 02:00:00           1   
       b             7.0 2016-02-26 12:00:00 2016-02-26 12:00:00           1   
       e             4.5 2016-02-25 06:00:00 2016-02-25 16:00:00           2   
3      b             8.5 2016-02-26 22:00:00 2016-02-27 08:00:00           2   
       d            10.0 2016-02-27 18:00:00 2016-02-27 18:00:00           1   

                long_mean  
ser_no CTRY_NM             
1      a             21.5  
       b             23.0  
2      a             26.0  
       b             27.0  
       e             24.5  
3      b             28.5  
       d             30.0  

答案 1 :(得分:1)

在您的活动onCreate()中添加此内容:

protected void onCreate(Bundle savedInstanceState) {

    //...
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    boolean isNight = hour < 6 || hour > 18;

    int currentDrawable = isNight ? R.drawable.night : R.drawable.day;
    View decorView = getWindow().getDecorView();
    Drawable drawable = ContextCompat.getDrawable(this, currentDrawable);
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        decorView.setBackgroundDrawable(drawable);
    else
        decorView.setBackground(drawable);
}      

希望这会有所帮助!!