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
原创粉丝点击