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

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. 【LeetCode OJ】Swap Nodes in Pairs

    题目:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2 ...

  2. 原创:Eclipse安装Eclipse Color Themes插件后,编辑器背景颜色被改变

    如题,卸载Eclipse Color Themes插件后,背景颜色还是白色,蛋疼,修改.metadata\.plugins\org.eclipse.core.runtime\.settings中的or ...

  3. 【sql进阶】查询每天、每个设备的第一条数据

    需求如下 每个设备(不同DeviceID).每天会向数据库插入多条数据,求每天.每个设备插入的第一条数据. 下面SQL中的 ShareRecommendID 类比不同设备的DeviceID. ROW_ ...

  4. 【truffle】Error: `truffle init` no longer accepts a project template name as an argument.

    下载范例工程时候.使用命令报错 truffle init webpack 错误如下: Error: `truffle init` no longer accepts a project templat ...

  5. Android开发进阶从小工到专家之性能优化

  6. Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]

    lxml --->首字母是字母l,不是数字1 lxml 2.x : https://pypi.python.org/pypi/lxml/2.3 1xml官网:http://lxml.de/ 一 ...

  7. 正则表达式取querystring

    var s = decodeURIComponent((new RegExp('[?|&]userid=([^&;]+?)(&|#|;|$)').exec(location.h ...

  8. php curl-class post

    use \Curl\Curl; $curl = new Curl();$curl->setHeader('Content-Type', 'application/json');$curl-> ...

  9. 题目1042:Coincidence(最长公共子序列 dp题目)

    题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  10. jenkins与SonarQube集成

    一.SonarQube 我的理解是,SonarQube就是一个对代码进行分析的平台,其功能可以通过插件扩展.支持多种语言,也支持静态代码检查.发现潜在bug等. 以下是参考信息: 维基百科:https ...