有的场景下。我们须要把手机屏幕方向改变,以下是我写的一个样例。

xml页面文件:

<RelativeLayout
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"

    tools:context=".MainActivity"
>

    <Button

        android:id="@+id/btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:text="改动屏幕方向"
/>

</RelativeLayout>

页面java文件:

package com.example.chcfg;

import android.os.Bundle;

import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.content.res.Configuration;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity
extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener() 

{

public
void onClick(View source) {

//
TODO Auto-generated method stub

Configuration config = getResources().getConfiguration();

if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)

{

//设置为竖屏

MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

//假设当前是竖屏

if(config.orientation == Configuration.ORIENTATION_PORTRAIT)

{

MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

}

});

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

//
TODO Auto-generated method stub

super.onConfigurationChanged(newConfig);

String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ?
"横向屏幕" : "纵向屏幕";

Toast.makeText(this,
"系统屏幕方向发生改变\n改动后的方向为:" + screen , Toast.LENGTH_LONG).show();

}

}

MainFest xml文件:

<?

xml
version="1.0"
encoding="utf-8"?>

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.chcfg"

    android:versionCode="1"

    android:versionName="1.0"
>

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="18"
/>

    <uses-permission
android:name="android.permission.CHANGE_CONFIGURATION"/>

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme"
>

        <activity

            android:name="com.example.chcfg.MainActivity"

            android:label="@string/app_name"

            android:configChanges="orientation">

            <intent-filter>

                <action
android:name="android.intent.action.MAIN"
/>

                <category
android:name="android.intent.category.LAUNCHER"
/>

            </intent-filter>

        </activity>

    </application>

</manifest>

个人辛勤劳动成果,如有转载,请注明出处,谢谢。

Android开发之改动屏幕方向的更多相关文章

  1. Cocos2d-x 3.0 Android改动APK名、更改图标、改动屏幕方向、改动版本,一些须要注意的问题

    非常多新手程序员做出一个游戏后,编译成apk安装在手机上.却发现安装程序名和游戏图标都是Cocos2dx默认的,并且默认屏幕方向是横向.那么须要怎么才干改动为自己想要的呢? 打开你创建的project ...

  2. Android 开发中的屏幕适配技术详解

    本文主要介绍Android开发中比较头疼繁琐的一个问题-屏幕适配问题.主要从适配原因.基本核心概念.适配方法等方面介详细 介绍从而是的深入或者进一步对Android屏幕适配技术的掌握和理解. 真题园网 ...

  3. Android开发——用户在屏幕上的手势识别

    个定点决定.四个属性分别为left(1),top(2),right(3),bottom(4). 数字为图上标出的距离.显然这四个属性是相对于父容器来定的,均可以通过get()方法获取. 因此很容易得出 ...

  4. Android开发系列之屏幕密度和单位转换

    由于Android的开源性,所以目前市面上面Android手机的分辨率特别多,这样的话就给我适配带来了一定的难度.要想做好适配,我们首先应该明白什么是分辨率.PPI.屏幕大小等概念,还有在不同的屏幕密 ...

  5. Android实现动态改变屏幕方向(Landscape & Portrait)

    1.AndroidManifest.xml: <activity> android:screenOrientation="portrait" ... 2.xx.java ...

  6. Android开发初期之后怎么提升?怎么才能叫精通?方向在哪?

    hi大头鬼hi Android开发专家     先mark一下,好多人我发现始终停留在两三年的水平上没有突破. 另外还有一个误区就是越底层越牛逼 第三个就是,我认识的大部分所谓的做过rom开发的对fr ...

  7. Android开发工具资料Android Manifest 权限描述大全 随时随地查询权限描述。

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985转载请说明出处. 在文章最后面赠送了markdown格式的此文章内容下载.(不要再问问什么用markdo ...

  8. Android开发11——手机横屏和竖屏与android:configChanges

    目前大多数手机都支持重力感应随之而来的就是屏幕方向改变的问题.对应普通开发者来说屏幕的随意改变也会带来困扰.在Google自带的doc里可以看到,如果设备的配置(Resources.Configura ...

  9. Android开发没有一技之长就废了吗?

    写在前面的话 不知你发现没有,人到中年之后,时间流逝的速度仿佛越来越快? 还记得毕业那会儿,我们怀着新鲜和好奇踏进了职场,那个时候每天都是满满的挑战和需要学习的东西,感觉时间过得真慢啊: 不知道从什么 ...

随机推荐

  1. N76E003之IIC

    I2C 总线提供了一种串行通信方式,用在 MCU 与 EEPROM,LCD模块,温度传感器等等之间控制.I2C 用两条线 (数据线SDA 和时钟线 SCL) 在设备间传输数据.I2C 总线用作主机与从 ...

  2. MVC的路由设置【转】

    转,MVC的路由设置. 后端获取路由里面action的参数,函数需要设置相同的参数名称才行. routes.MapRoute( "Default", "{controll ...

  3. 对Android 开发者有益的 40 条优化建议(转)

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...

  4. Python在mysql中进行操作是十分容易和简洁的

    首先声明一下,我用的是Windows系统! 1.在Python中对mysql数据库进行操作首先要导入pymysql模块,默认情况下,Python中是没有安装这个模块的, 可以在Windows的命令行中 ...

  5. 【大数据系列】windows下连接Linux环境开发

    一.配置文件 1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> ...

  6. NC 的简单使用

    netcat被誉为网络安全界的’瑞士军刀’,相信没有什么人不认识它吧……一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松 ...

  7. sencha touch 压缩js,css遇到的问题

    在使用工具压缩css和jss时,我遇到了以下问题 1. showBtn: { tap: function (t, value) { this.redirectTo(t.config.goto); } ...

  8. C程序设计语言习题(3-3)

    编写函数expand(s1,s2), 将字符串s1中类似于a-z一类的速记符号在字符串s2中扩展为等价的完整列表abc……xyz.该函数可以处理大小写字母和数字,并可以处理a-b-c.a-z0-9与a ...

  9. C# 二维码 ThoughtWorks.QRCode.dll

    ThoughtWorks.QRCode.dll: 1.ThoughtWorks.QRCode.dll 2.通过 NuGet 添加 后台代码: using System; using System.Dr ...

  10. Android aapt使用小结

    Android打包成Apk后,其实是一个压缩文件,我们用winrar打开也能看到里面的文件结构.还能看到AndroidManifest.但是里面的内容有点问题. 不知道是因为加密还是Android就是 ...