Android——例子:屏幕切换
生活随笔
收集整理的這篇文章主要介紹了
Android——例子:屏幕切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?效果圖如下:
?
????????????????????????
?
Xml文件代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/myBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="點擊切換到豎屏"/><ImageView android:id="@+id/showImage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/aaa"/> </LinearLayout>Java代碼
package com.example.clickproject;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.ImageView;public class ScreenActivity extends Activity {private Button myBtn = null;private ImageView showImage = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.screen);myBtn = (Button) findViewById(R.id.myBtn);showImage = (ImageView) findViewById(R.id.showImage);myBtn.setOnClickListener(new MyOnClickListenerImpl());}private class MyOnClickListenerImpl implements OnClickListener { // 單擊事件public void onClick(View v) {if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {// 無法進行畫面的旋轉ScreenActivity.this.myBtn.setText("錯誤:無法改變屏幕方向。");} else {if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { // 現在的方向是橫屏顯示ScreenActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 變為豎屏顯示} else if (ScreenActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // 如果為豎屏顯示ScreenActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 變為橫屏顯示 }}}}@Overridepublic void onConfigurationChanged(Configuration newConfig) { // 表示的是系統設置修改的時候觸發if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 現在的屏幕方向是橫屏ScreenActivity.this.myBtn.setText("改變屏幕方向為豎屏顯示(當前為橫屏顯示)");ScreenActivity.this.showImage.setImageResource(R.drawable.aaa);// 顯示橫屏圖片} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // 現在豎屏ScreenActivity.this.myBtn.setText("改變屏幕方向為豎屏顯示(當前為橫屏顯示)");ScreenActivity.this.showImage.setImageResource(R.drawable.aaa);// 顯示豎屏圖片 }super.onConfigurationChanged(newConfig);} }AndroidMainifest.xml文件中寫入以下代碼:
<activityandroid:name="com.example.clickproject.ScreenActivity"android:label="@string/app_name"android:screenOrientation="portrait" android:configChanges="orientation|keyboard"><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.persmission.CHANGE_CONFIGURATION" />?
轉載于:https://www.cnblogs.com/yangzhenlong/p/3217384.html
總結
以上是生活随笔為你收集整理的Android——例子:屏幕切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++类中使用new及delete小例子
- 下一篇: Android Animation学习(