日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android——例子:屏幕切换

發布時間:2024/4/15 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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——例子:屏幕切换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。