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给销毁了,但又重新执行加载),怎么使屏幕横竖屏切换时,当前的 ...
随机推荐
- jQuery源码-dom操作之jQuery.fn.text
写在前面 jQuery.fn.text在jQuery是个使用频率比较高的接口,它的作用无非是设置/获取dom节点的内容文本,下文会通过几个简单的例子来说明.text()接口的使用,以及最后会对源码进行 ...
- 【CISP笔记】安全漏洞与恶意代码(2)
恶意代码自我保护 进程保护 进程守护 超级权限 检测对抗 反动态调试 反静态调试 恶意代码检测技术 特征码扫描 沙箱技术 行为检测 恶意代码分析技术 静态分析 需要实际执行恶意代码,它通过对其二进制文 ...
- 繁华模拟赛day8 牛栏
/* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...
- jquery mobile cannot be created in a document with origin 'null' and URL
jquery mobile cannot be created in a document with origin 'null' and URL http://zhidao.baidu.com/lin ...
- 深入理解Java虚拟机之读书笔记一 自动内存管理机制
一.运行时数据区域 1.程序计数器是线程的私有空间,每个线程都有.针对线程执行的是Java代码还是Native代码有两种取值,Java代码时:虚拟机字节码指令的地址:Native代码时:计数值为Und ...
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- SSH-Struts第三弹:传智播客视频教程第一天上午的笔记
一. 框架概述1.三大框架 : 是企业主流 JavaEE 开发的一套架构 Struts2 + Spring + Hibernate 2. 什么是框架?为什么要学框架 ?框架 是 实现部分功能的代码 ( ...
- Apache 无法启动
本人是做前端开发的,对后台程序不太熟悉,也就以前学过一点.net.但现在都忘记的差不多了.最近在公司,经理给了我一个管理工具dedecms,我刚开始看的时候完全不懂这是什么东西,之前都没听说过(本人见 ...
- 思科 vlan 相关操作
添加或者修改VLAN Switch(config)# vlan vlan-id Switch(config-vlan)# name vlan-name 删除VLAN Switch(config)# n ...
- 架构Android App总结
历时两个多月,自己架构的一个App快要完成了,有很多可以总结的地方: 1, 各个模块尽可能独立,不要直接调用,用消息机制解耦.包括页面跳转不要直接startActivity,而是用消息跳转:业务模块请 ...