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

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. ARM9通过NFS挂载根文件系统

    当开发板启动以后可以通过在超级终端发送命令来配置NFS. 首先得给开发板一个IP地址,用下面的命令配置即可: #ifconfig 192.168.0.10 经过上面的配置以后在各自的终端中应该都能PI ...

  2. 执行上下文、this

    1.js中的执行上下文或者执行环境:execution context,简称EC; 2. console.log(a);//undefined var a=200; fn('lili'); funct ...

  3. 使用session防止表单进行重复提交

    我们都知道可以通过js的方法来实现防止表单重复提交,但是js只适用于“在网络延迟的情况下让用户有时间点击多次submit按钮导致表单重复提交” 的情况下进行操作, 那如果碰到“表单提交后用户点击[刷新 ...

  4. make: Warning: File `Makefile' has modification time 17 s in the future

    linux下,make makefile文件的时候报警告: make: Warning: File `Makefile' has modification time 17 s in the futur ...

  5. STL源码剖析—顺序容器

    一.vector 1.vector简介: vector的数据安排及其操作方式与数组非常相似,微小的差别在于空间的使用,数组是静态空间,一旦配置了就不能改变.vector是动态空间,随着元素的加入,它的 ...

  6. Matlab练习——寻找完全数

    clc; clear; wq = []; : sum = ; k = ; : i / sum = sum + j; end end == i wq=[wq i]; end end disp(['2至1 ...

  7. 如何修改 VIM 制表符的空格数?

     想修改一下编辑器vi里的制表符(Tab)的空格数.因为它默认的太长(默认是8个空格).  在网上搜到了这篇文章http://my.oschina.net/captaintheron/blog/515 ...

  8. Android 手机震动

    1.添加震动权限 <uses-permission android:name="android.permission.VIBRATE"/> 2.获取震动服务 Vibra ...

  9. 浏览器 User Agent字符串列表

    http://www.73207.com/useragent/ http://www.73207.com/useragent/pages/internet-2520explorer/index.htm ...

  10. VSCode集成TypeScript编译

    先安装github客户端和nodeJS客户端吧,直接去官网下载,nodeJS客户端安装完就集成了npm; 查看是否成功: git version  node -v npm-v 安装TypeScript ...