很多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应用主题与横竖屏的切换的更多相关文章

  1. Android开发之Activity横竖屏切换生命周期重建问题

    当进行横竖屏切换的时候Activity的生命周期会重建,从而导致Activity崩溃等问题,为了避免这一问题,需要在AndroidManifest.xml文件中设置: <activity and ...

  2. 在Android中自动实现横竖屏切换的问题

    http://developer.android.com/training/basics/supporting-devices/screens.html参照Google推荐的做法 在你项目的res 文 ...

  3. android界面横屏和竖屏的切换

    关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...

  4. Android之怎样强制横竖屏显示

    2  强制横屏竖屏显示(不依据手机传感器调整) 在清单文件AndroidManifest.xml中 在<activity 里面增加: android:screenOrientation=&quo ...

  5. Android应用:横竖屏切换总结

    眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...

  6. Android横竖屏切换及其对应布局加载问题

    第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...

  7. Android横竖屏切换重载问题与小结

    (转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com ...

  8. Android 横竖屏切换小结

    (自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...

  9. android 横竖屏切换

    解决Android手机 屏幕横竖屏切换 Android中当屏幕横竖屏切换时,Activity的生命周期是重新加载(说明当前的Activity给销毁了,但又重新执行加载),怎么使屏幕横竖屏切换时,当前的 ...

随机推荐

  1. 2015年12月28日 Java基础系列(六)流

    2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流

  2. sql批量更换dedecms文章来源和作者

    前面写了一篇修改dedecms默认文章来源 "未知"改为关键词,那个是修改以后发布的文章“来源”才会变成自己设定的关键词,如果修改之前已经有很多文章了,那些文章“来源”还是显示“未 ...

  3. C#之规格说明书

    1. Statement My Note Problem Statement Version 1.0   Revision History Date Issue Description Author ...

  4. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  5. FineUI第十四天---布局之垂直布局和水平布局

    布局值水平布局和垂直布局 垂直盒子布局和水平盒子布局非常灵活易用,在很大程度上能够取代锚点布局,行布局和列布局. 1.垂直盒子布局: BoxConfigAlign:控制子容器的的尺寸 Start:位于 ...

  6. Java多线程基础知识(三)

    一. 管道输入/输出流 它和其它文件输入/输出流或网络输入/输出流的不同之处,它主要是线程之间的数据传输,而传输的媒介是内存. 管道输入/输出流主要包含四中实现: 1. PipedOutputStre ...

  7. php预定义常量$_SERVER

    1.需求 了解预定义常量$_SERVER 2.属性 $_SERVER['REQUEST_URI'] //URI 用来指定要访问的页面.例如 "/index.html" $_SERV ...

  8. Hashtable和HashMap的区别举例

    我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable ...

  9. spring ext 跨域

    read方法中调用的response对象是父类BaseController的一个成员变量. spring默认bean的生命周期Score为singleton单例模式. 当多线程并发使用同一bean, ...

  10. 插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法

    SQL Server 2008 R2 x64 下 事情是这样的, 使用Navcate的数据导出备份工具导出表的记录 INSERT INTO [xxx_users] VALUES (1, 'tom ', ...