編輯:關於 Android 程式設計
某些場景下需要主動切換手機螢幕方向,以下是一個簡單範例。
佈局中放一個觸發切換的按鈕:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="修改屏幕方向" />
</RelativeLayout>
點擊按鈕時,先讀取目前方向,若是橫向就設為縱向,反之亦然;並在 onConfigurationChanged 中提示方向已改變:
package com.example.chcfg;
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.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View source) {
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 設為豎屏
MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "橫向屏幕" : "縱向屏幕";
Toast.makeText(this, "系統屏幕方向發生改變\n修改後的方向為:" + screen, Toast.LENGTH_LONG).show();
}
}
需要加上 CHANGE_CONFIGURATION 權限,並在 Activity 上設定 android:configChanges="orientation",讓方向變更時由程式自行處理而不重建 Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.chcfg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.chcfg.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
個人辛勤勞動成果,如有轉載,請注明出處,謝謝。
RxAndroid 快速入門:從 RxJava 概念到實戰範例
一、RxJava 是什麼 官方對 RxJava 的定義是:一個在 Java 虛擬機上,以可觀察序列來組裝非同步、事件驅動程式的函式庫。一句話概括就是「非同步」——換句話說
Android 高斯模糊效果實現及低版本兼容方案
一、效果演示 項目中需要用到高斯模糊效果。查閱資料、權衡性能之後,我最終選擇了 Android 自帶的 RenderScript 庫來實現。網上關於 RenderScri
Android 屬性動畫入門(二):從原理到 API 上手
上一篇聊完了逐幀動畫與補間動畫,這篇把重心放到屬性動畫上。它是目前最靈活、也最常用的動畫方案,可配置的項目包括: Duration:動畫總時長,預設 300ms; Ti
Android ListView 深入解析與多種適配器
ListView 是 Android 中最重要的列表元件之一。它可以以列表形式呈現文字、圖片與控制項,並依資料長度自動捲動。本文從基本用法到自訂按鈕列表,完整梳理 Lis