首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android动态改动屏幕方向

2012-06-27 
android动态更改屏幕方向在androd中要通过程序改变屏幕显示的方向,必须覆盖setRequestOrientation()方法,

android动态更改屏幕方向

在androd中要通过程序改变屏幕显示的方向,必须覆盖setRequestOrientation()方法,若要取得目前屏幕方向则需要用到getRequestOrientation()方法。
本例子实现了简单的改变屏幕方向,通过一个按钮点击事件,判断当前的屏幕方向,例如竖排(PORTRAIT),则改为横排(LANDSCAPE);
本程序重写setRequestOrientation()方法,起目的是为了要捕捉设置屏幕放向的同时所触发的事件,并在更改的时候以Toast显示要更改的方向。
程序一开始闲判断getRequestOrentation()的值是否为-1,表示在Activity属性里没有设置android:screenOrientation="portrait",这意味着即使点击按钮也无法判断屏幕的方向,不会进行更改方向的事件了。

布局文件main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView  android:id="@+id/textView01"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text=""    /><Button android:id="@+id/button01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="@string/button_text01"/></LinearLayout>

?OrientationActivity.java

public class OrientationActivity extends Activity {private TextView tv;private Button button;private Resources rs;private Display display;/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        display = getWindow().getWindowManager().getDefaultDisplay();        tv = (TextView) this.findViewById(R.id.textView01);        button = (Button) this.findViewById(R.id.button01);        rs = this.getResources();        if(getRequestedOrientation() == -1){        tv.setText(rs.getText(R.string.err_msg));        }                button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {/* 方法一 */if(getRequestedOrientation() == -1){tv.setText(rs.getText(R.string.err_msg));}else{/* 方法一 *///if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){///* 若当前为横排则改为竖排 *///setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//}else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){///* 若当前为竖排则改为横排 *///setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//}int h = display.getHeight();int w = display.getWidth();if(w < h){setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}else{setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}}}                });    }        /* (non-Javadoc)     * @see android.app.Activity#setRequestedOrientation(int)     */    @Override    public void setRequestedOrientation(int requestedOrientation) {    switch (requestedOrientation) {case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:MakeTextToast.showToast(OrientationActivity.this,rs.getText(R.string.msg_2).toString() , true);break;case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:MakeTextToast.showToast(OrientationActivity.this,rs.getText(R.string.msg_2).toString() , true);break;default:break;}    super.setRequestedOrientation(requestedOrientation);    }        /* (non-Javadoc)     * @see android.app.Activity#getRequestedOrientation()     */    @Override    public int getRequestedOrientation() {    // TODO Auto-generated method stub    return super.getRequestedOrientation();    }}

?MakeTextToast.java

public class MakeTextToast {/** * 显示Toast * @param context * @param msg 信息 * @param isLong 显示时间长短 */public static void showToast(Context context,String msg,boolean isLong){if(isLong){Toast.makeText(context, msg, Toast.LENGTH_LONG).show();}else{Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();}}}

?

Manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".OrientationActivity"                  android:label="@string/app_name"                  android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

?运行结果图:

竖排:
android动态改动屏幕方向

?

横排:
?
android动态改动屏幕方向
?

热点排行