如何檢視橫屏模式下的活動?
此示例演示如何檢視橫屏模式下的活動。
第 1 步 − 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,然後填寫所有必需的詳細資訊以建立一個新專案。
第 2 步 − 在 res/layout/activity_main.xml 中新增以下程式碼。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/conf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
在上面的程式碼中,我們採用了文字檢視來顯示模式名稱。
第 3 步− 在 java/MainActivity.xml 中新增以下程式碼
package com.example.myapplication;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView conf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conf = findViewById(R.id.conf);
}
@Override
public void onConfigurationChanged(Configuration _newConfig) {
super.onConfigurationChanged(_newConfig);
if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
conf.setText("It is landscape");
}
}
}第 3 步− 在 AndroidManifest.xml 中新增以下程式碼
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
我們嘗試執行您的應用程式。我假設您已將實際的 Android 移動裝置與計算機連線起來。要從 Android Studio 執行應用程式,請開啟您專案的某個活動檔案,然後單擊執行
工具欄上的圖示。選擇您的移動裝置作為選項,然後檢查將顯示您預設螢幕的移動裝置 -

單擊 此處 下載專案程式碼
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP