Android应用主题与横竖屏的切换
很多App,现在都具有了横竖屏切换的功能,或者说“白天”和“黑夜”主题的切换。
实现起来也非常简单。主要需要注意的是,在切换的同时,页面的数据不能丢失,不然给用户的体验就会大打折扣了。
横竖屏切换效果图:

当手机倒置的时候,屏幕会自动切换。并且不管怎么倒置,onCreate生命周期都只执行了1次。
现在看下布局代码吧,一共两个布局页面,互相切换。
layout_portait.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/holo_blue_light"
android:orientation="vertical"
tools:context="com.kevin.layoutskin.MainActivity"> <Button
android:id="@+id/btn_qiehuan_portait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换主题(非横竖屏)" /> <TextView
android:id="@+id/tv_portait"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Layout_Portait竖屏"
android:gravity="center"
android:textSize="25sp"/> </LinearLayout>
layout_landscape.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@android:color/holo_green_dark"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kevin.layoutskin.MainActivity"> <Button
android:id="@+id/btn_qiehuan_landscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换主题(非横竖屏)" /> <TextView
android:id="@+id/tv_landscape"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Layout_Landscape横屏"
android:textSize="25sp" /> </LinearLayout>
主类代码:MainActivity.class
package com.kevin.layoutskin; import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button1;
private Button button2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_portait);
Log.e("TAG", "onCreate"); button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
button1.setOnClickListener(this);
} @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("TAG", "onConfigurationChanged");
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.layout_portait);
button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
button1.setOnClickListener(this);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.layout_landscape);
button2 = (Button) findViewById(R.id.btn_qiehuan_landscape);
button2.setOnClickListener(this);
}
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_qiehuan_portait:
setContentView(R.layout.layout_landscape);
button2 = (Button) findViewById(R.id.btn_qiehuan_landscape);
button2.setOnClickListener(this);
break;
case R.id.btn_qiehuan_landscape:
setContentView(R.layout.layout_portait);
button1 = (Button) findViewById(R.id.btn_qiehuan_portait);
button1.setOnClickListener(this);
break;
}
}
}
主类里面的点击事件,之所以给他加了点击事件,是为了实现主题的切换,并非是横竖屏切换。 在横竖屏切换这块要注意的是:需要在清单文件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" />
完事!
Android应用主题与横竖屏的切换的更多相关文章
- Android开发之Activity横竖屏切换生命周期重建问题
当进行横竖屏切换的时候Activity的生命周期会重建,从而导致Activity崩溃等问题,为了避免这一问题,需要在AndroidManifest.xml文件中设置: <activity and ...
- 在Android中自动实现横竖屏切换的问题
http://developer.android.com/training/basics/supporting-devices/screens.html参照Google推荐的做法 在你项目的res 文 ...
- android界面横屏和竖屏的切换
关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...
- Android之怎样强制横竖屏显示
2 强制横屏竖屏显示(不依据手机传感器调整) 在清单文件AndroidManifest.xml中 在<activity 里面增加: android:screenOrientation=&quo ...
- Android应用:横竖屏切换总结
眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...
- Android横竖屏切换及其对应布局加载问题
第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...
- Android横竖屏切换重载问题与小结
(转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com ...
- Android 横竖屏切换小结
(自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...
- android 横竖屏切换
解决Android手机 屏幕横竖屏切换 Android中当屏幕横竖屏切换时,Activity的生命周期是重新加载(说明当前的Activity给销毁了,但又重新执行加载),怎么使屏幕横竖屏切换时,当前的 ...
随机推荐
- Collection类相关总结
集合类的框架如下: Collection(接口) List(接口):允许重复. ArrayList Vector LinkedList Se ...
- cas
cas配置ladp地址:
- Go - 内置函数大全
Package builtin import "builtin" Overview Index Overview ▾ Package builtin provides docume ...
- Hibernate面试题
一.谈一谈Hibernate的缓存机制 1.一级缓存: 内部缓存存在于HIbernate中又叫一级缓存,他属于应用事务级缓存. 2.二级缓存: 01.应用级缓存. 02.分布式缓存. 条件:数据不会被 ...
- 第五天 loadmore
mutating func loadFresh(completion: (result: APIResult<DeserializedType>) -> ()) -> Canc ...
- gcc 和g++区别
gcc和g++都是GNU的一个编译器;这两者的区别:1.从源文件上看,对于文件后缀(扩展名)为.c的test.c文件,gcc会把它看成是C程序,而g++则会把它看成是C++程序;而对于文件后缀(扩展名 ...
- JS获取地址栏参数
获取地址栏参数(其中name为你所需要的参数值) function GetQueryString(name) { var reg = new RegExp("(^|&)" ...
- JVM内存分析工具MAT使用
1. 首先去官网下载MAT软件,路径如下: 点击打开链接 2. 将heap dump文件打开即可分析.
- Jrebel是一套开发环境,用来实现热部署
http://truemylife.iteye.com/blog/1140921 背景与愿景:开发环境下,tomcat对热布署的支持还不够全面,致使开发人员浪费大量时间在重起服务上.为了提高开发效率, ...
- win7平台下React-Native Android:Unable to upload some APKs
一.问题描述 根据网络上的Win7平台下React-native配置教程配置好开发环境的过程中,在艰难进行到react-native run-android这一步时,发现一直出现错误,截图如下: 错误 ...