很多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. 解析posix与perl标准的正则表达式区别 ---PHP

        正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式.正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系 ...

  2. 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...

  3. iOS9 Universal Link实现

    先来贴几个比较全面的帖子,看完就差不多了. 1   iOS 9 通用链接(Universal Links) 帖子注意项非常重要,仔细阅读. 自己补充几点: 1 服务器上配置的json文件apple-a ...

  4. ios修改textField的placeholder的字体颜色和大小

    textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...

  5. HDOJ 4717 The Moving Points

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. UESTC 1851 Kings on a Chessboard

    状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...

  7. Mysql InnoDB行锁实现方式

    Mysql InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点 ...

  8. PPPoE名词解释

    PPPoE拔号的发现阶段(Discovery): PPPoE的发现阶段一共分为4步. 分别是: PADI(PPPoE Active Discovery Initiation) PADO(PPPoE A ...

  9. mac 下 xampp 多域名 多站点 多虚拟主机 配置

    前言:最近用mac工作了,需要搭建个调试前段程序的站点,选了xampp,需求是能同时运行多个站点,多个域名,目录自定义,网上找了好多资料,都感觉有些不符合心意,且复制文确实很多,甚至有些没实践过的在乱 ...

  10. Ajax前台调用后台方法、AJAX Pro2(回调函数)

    //获取分店 function cityResult() { if (cityName != "") { $("#ddlcity_").find("o ...