標籤:android style http io ar color os sp java
Configuration類 用於擷取手機上的裝置的配置資訊,包括使用者配置項和系統的動態設定項
可通過Activity的如下方法獲得:
Configuration cfg = getResource().getConfiguration();
屬性
public float fontScale; 擷取目前使用者設定的字型的縮放因子
public int mcc; 擷取移動訊號的國家碼
public int mnc;擷取移動訊號的網路碼
public Locale locale;擷取使用者當前的Local
public boolean userSetLocale;//Locale should persist on setting. This is hidden because it is really * questionable whether this is the right way to expose the functionality.
public int keyboardHidden: 擷取當前裝置所關聯的鍵盤類型。
KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通電腦鍵盤)、
KEYBOARD_12KEY(只有12個鍵的小鍵盤)
public int keyboardHidden; 該屬性返回一個標誌當前鍵盤是否可用:(該屬性不僅會判斷系統的硬體鍵盤,還會判斷系統的軟體機碼盤(位於螢幕上的))
1.如果系統的硬體鍵盤不可用,但軟體機碼盤可用,會返回KEYBOARDHIDDEN_NO()、
2.只有當兩個鍵盤都不可用時,才返回KEYBOARDHODDEN_YES
public int orientation:擷取系統螢幕的方向,:ORIENTATION_LANDSPACE(橫屏)、ORIENTATION_PORTRAIT(豎向螢幕)、ORIENTATION_SQUARE(方形螢幕)等屬性
現在我們實現一個簡單的螢幕切換(通過點擊一個普通的button來實現):
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="142dp" android:text="點擊改變螢幕方向" /></RelativeLayout>
Main.java
public class Main extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button b = (Button) findViewById(R.id.button1);b.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Configuration cfg = getResources().getConfiguration();if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE) {Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}}});}/** * 在AndroidManifest.xml檔案中設定 * android:configChanges="orientation" * 螢幕方向改變,會回調這個方法 */@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);String str = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "橫屏顯示": "豎屏顯示";Toast.makeText(this, str, Toast.LENGTH_LONG).show();}}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.main" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="12" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 設定activity可以監聽到螢幕方向改變的事件 --> <activity android:name=".Main" 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>
! !!設定檔中的:
android:targetSdkVersion="12"
應為12,設定得過高,onConfigurationChanged()方法不會被觸發。
android實現簡單的橫豎屏之間的切換