Android横竖屏切换
来源:互联网 发布:淘宝上的zuzu是正品吗 编辑:程序博客网 时间:2024/06/05 18:39
功能: Activity上就只有一个button组件,当点击button时切换屏幕的方向。
main.xml:
<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="切换为横屏(当前为竖屏)"/></LinearLayout></span></span>
主程序代码:
<span style="font-size:18px;">package com.sample.switchorientation;import android.app.Activity;import android.content.pm.ActivityInfo;import android.content.res.Configuration;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SwitchOrientationActivity extends Activity {private Button btn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubSystem.out.println("or"+getRequestedOrientation());/** * getRequestedOrientation():Returns an orientation constant as used in ActivityInfo.screenOrientation */if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED){//如果屏幕没有指定显示方向btn.setText("没有指定屏幕方向");}else{if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){//如果当前获得的方向为横向setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//则将屏幕方向该为纵向}else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){//如果当前获得的方向为纵向setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//则将屏幕方向该为横向}}}}); } /** * 当指定的系统参数发生改变时触发此事件 * 方法的功能比较简单就是改变按钮上的文字 */@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){btn.setText("改为竖屏(当前为横屏)");}else{btn.setText("改为横屏(当前为竖屏)");}}}</span>
AndroidManifest.xml:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample.switchorientation" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".SwitchOrientationActivity" <!-- 指定当哪些系统参数改变时,onConfigurationChanged()方法会被触发 --> android:configChanges="orientation|keyboard" <!-- 设定当前屏幕的方向 --> android:screenOrientation="portrait"> <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- 让用户活得修改系统参数的权限 --> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/></manifest></span>
结果:
0 0
- Android 横竖屏切换
- Android横竖屏'切换
- Android横竖屏切换
- Android 横竖屏切换
- Android横竖屏切换
- android 横竖屏切换
- android 横竖屏切换
- Android横竖屏切换
- Android 横竖屏切换
- android切换横竖屏
- android 横竖屏切换
- Android横竖屏切换
- android 横竖屏切换
- Android横竖屏切换
- android 横竖屏切换
- Android横竖屏切换
- Android横竖屏切换
- Android横竖屏切换
- h264基础知识梳理
- 数组应用
- a包含<span><img> 导致超链接失效的bug
- Nim游戏博弈(收集完全版)
- windowsNote(小便签)
- Android横竖屏切换
- hdu 5011 (nim博弈模版)
- nginx+tomcat+session共享
- 任意整数 ,输出从大到小,连续的去最大最小。 最大长度不超过1024;
- Mac 中 mail的使用POP IMAP SMTP
- 移植gdb6.6到arm平台
- 面试题总结
- Android---3种方式限制EditView输入字数
- (待切)POJ 1926 Pollution 模拟/并查集