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

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. C语言从零开始(十四)-字符串处理

    在软件开发过程中,字符串的操作相当频繁.在标准C语言库中提供了很多字符串处理的函数.今天我们来介绍一些常用的字符串处理函数.1. 字符串输入输出1.1 printf() scanf() 之前我们学习过 ...

  2. mysql存储过程中使用事务

    DROP PROCEDURE IF EXISTS test_sp1 CREATE PROCEDURE test_sp1( ) BEGIN ; ; START TRANSACTION; INSERT I ...

  3. bigdecimal 与long int 之间转换

    BigDecimal与Long.int之间的互换 在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigD ...

  4. Windows 7笔记本创建wifi热点供手机上网教程

    Windows 7笔记本创建wifi热点供手机上网教程 | 浏览:60606 | 更新:2012-07-19 11:48 | 标签:笔记本 wifi 1 2 3 4 5 6 7 分步阅读 用智能手机的 ...

  5. javascript的fn方法(转)

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...

  6. goldengate 过滤对某张表的复制操作

    在复制进程中配置下面的参数可以实现对一个用户下的某些表进行过滤,在复制的时候 不做任何操作. MAPEXCLUDE: Valid for Replicat Use the MAPEXCLUDE par ...

  7. Elasticsearch学习之深入搜索四 --- cross-fields搜索

    1. cross-fields搜索 一个唯一标识,跨了多个field.比如一个人,标识,是姓名:一个建筑,它的标识是地址.姓名可以散落在多个field中,比如first_name和last_name中 ...

  8. JVM垃圾收集器组合--各种组合对应的虚拟机参数实践

    前言 相信很多人都看过下面这张图,(来自<深入理解Java虚拟机:JVM高级特性与最佳实践>) 在学完几种垃圾收集器类型及组合后,打算看看实际中程序用到的垃圾收集器. 但是在jconsol ...

  9. HP P2xxx/MSA SMI-S Provider

    HP P2xxx/MSA SMI-S Provider The HP MSA provider must be enabled before it can be monitored. For more ...

  10. node path的一些理解笔录

    例子假如我们有这样的文件结构: app/ -lib/ -common.js -model -task.js -test.js 执行代码: var path = require("path&q ...