Android应用主题与横竖屏的切换

很多App,现在都具有了横竖屏切换的功能,或者说“白天”和“黑夜”主题的切换。

实现起来也非常简单。主要需要注意的是,在切换的同时,页面的数据不能丢失,不然给用户的体验就会大打折扣了。

横竖屏切换效果图:

 

当手机倒置的时候,屏幕会自动切换。并且不管怎么倒置,onCreate生命周期都只执行了1次。

现在看下布局代码吧,一共两个布局页面,互相切换。

layout_portait.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     android:background="@android:color/holo_blue_light"
11     android:orientation="vertical"
12     tools:context="com.kevin.layoutskin.MainActivity">
13 
14     <Button
15         android:id="@+id/btn_qiehuan_portait"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="切换主题(非横竖屏)" />
19 
20     <TextView
21         android:id="@+id/tv_portait"
22         android:layout_width="match_parent"
23         android:layout_height="match_parent"
24         android:text="Layout_Portait竖屏"
25         android:gravity="center"
26         android:textSize="25sp"/>
27 
28 </LinearLayout>

layout_landscape.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="@android:color/holo_green_dark"
 7     android:orientation="vertical"
 8     android:paddingBottom="@dimen/activity_vertical_margin"
 9     android:paddingLeft="@dimen/activity_horizontal_margin"
10     android:paddingRight="@dimen/activity_horizontal_margin"
11     android:paddingTop="@dimen/activity_vertical_margin"
12     tools:context="com.kevin.layoutskin.MainActivity">
13 
14     <Button
15         android:id="@+id/btn_qiehuan_landscape"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="切换主题(非横竖屏)" />
19 
20     <TextView
21         android:id="@+id/tv_landscape"
22         android:layout_width="match_parent"
23         android:layout_height="match_parent"
24         android:gravity="center"
25         android:text="Layout_Landscape横屏"
26         android:textSize="25sp" />
27 
28 
29 </LinearLayout>

主类代码:MainActivity.class

 1 package com.kevin.layoutskin;
 2 
 3 import android.content.res.Configuration;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.util.Log;
 7 import android.view.View;
 8 import android.widget.Button;
 9 
10 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
11 
12     private Button button1;
13     private Button button2;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.layout_portait);
19         Log.e("TAG", "onCreate");
20 
21         button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
22         button1.setOnClickListener(this);
23     }
24 
25     @Override
26     public void onConfigurationChanged(Configuration newConfig) {
27         super.onConfigurationChanged(newConfig);
28         Log.e("TAG", "onConfigurationChanged");
29         if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
30             setContentView(R.layout.layout_portait);
31             button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
32             button1.setOnClickListener(this);
33         } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
34             setContentView(R.layout.layout_landscape);
35             button2 = (Button) findViewById(R.id.btn_qiehuan_landscape);
36             button2.setOnClickListener(this);
37         }
38     }
39 
40     @Override
41     public void onClick(View v) {
42         switch (v.getId()) {
43             case R.id.btn_qiehuan_portait:
44                 setContentView(R.layout.layout_landscape);
45                 button2 = (Button) findViewById(R.id.btn_qiehuan_landscape);
46                 button2.setOnClickListener(this);
47                 break;
48             case R.id.btn_qiehuan_landscape:
49                 setContentView(R.layout.layout_portait);
50                 button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
51                 button1.setOnClickListener(this);
52                 break;
53         }
54     }
55 }

主类里面的点击事件,之所以给他加了点击事件,是为了实现主题的切换,并非是横竖屏切换。  在横竖屏切换这块要注意的是:需要在清单文件manifests的对应Acitivity里面添加这行代码

android:configChanges="orientation|locale|screenSize">

当然,主要是需要“orientation”和“scereenSize”,而“local”是在本地化的一些改变时(比如语言变更啥的)会用它,4.0系统之后,需要“orientation”和“scereenSize”,之前只需要“orientation”就可以了。  遵循了这个规则,Activity的onCreate生命周期才只会执行1次,以后都不管怎么切换,都只会执行“onConfigurationChanged”方法。

最后,不要忘记添加权限:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />

完事!

原文地址:https://www.cnblogs.com/huolongluo/p/6043556.html