很多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. [转载]解析用户生命周期价值:LTV

    http://www.sykong.com/2014/07/23144 http://youxiputao.com/articles/1288 http://www.woshipm.com/opera ...

  2. 【转载】Ubuntu 系列安装 Docker

    系统要求 Docker 支持以下版本的Ubuntu操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu Wily 15.10 Ubuntu Trusty 14.04 (LTS) ...

  3. 收到的电邮附件为Winmail.dat?

    以下信息来源于微软帮助中心:您收到电子邮件,其中包含一个 winmail.dat 的附件.电子邮件被人使用的 Microsoft Outlook 发送给您.该邮件的格式是丰富文本格式 (RTF). 原 ...

  4. PHP的 Mysqli扩展库的多语句执行

    $mysqli->multi_query($sqls);     执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...

  5. HDOJ 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. Mac Pro 编译安装 Nginx 1.8.1

    #下载相关源码包,统一放到 /usr/local/src 目录下: http://nginx.org/download/nginx-1.8.1.tar.gz http://zlib.net/zlib- ...

  7. jQuery - 动态创建iframe并加载页面

    <html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...

  8. PHP Socket实现websocket(二)Socket函数

    PHP socket函数是调用系统的的Socket函数,可以参考C语言的socket函数. Socket函数:http://php.net/manual/en/book.sockets.php 服务器 ...

  9. 实现Android的不同精度的定位(基于网络和GPS)

    解决方案: 实现Android的不同精度的定位(基于网络和GPS) Android中的定位服务的相关类基本上都在android.location包中,其中位置服务管理器(LocationManager ...

  10. OpenGL图元的颜色属性

    OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 1. RGBA颜色RGBA模式中,每一个像素会保存以下数据:R值(红色分量).G值(绿色分量).B值(蓝色分量)和A值(alpha分 ...