我的裸機測試應用程序將啓動時的方向設置爲橫向。它有一個佈局和佈局端口目錄。每個目錄中都有一個簡單的佈局文件。它們包含一個文本視圖,其文本設置爲「橫向」或「縱向」。Android setContentView使用錯誤的佈局文件
在我的測試中,我開始在平板電腦上放入肖像的應用程序。通過這段代碼,我期望使用/layout/my_layout.xml中的佈局,但是會使用/layout-port/my_layout.xml。我也嘗試製作一個/佈局土地目錄,但仍然沒有運氣。
從我的主要活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start app with tablet in portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int o = this.getRequestedOrientation();
// o is 0, indicating landscape. Display shifted to landscape
setContentView(R.layout.my_layout);
// layout-port/my_layout.xml is loaded. INCORRECT I think
}
/layout/my_layout.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:background="#ff000000">
<TextView
android:id="@+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Landscape"
android:textSize="50sp"
android:textColor="#ffffffff"/>
</LinearLayout>
/layout-port/my_layout.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:background="#ff000000">
<TextView
android:id="@+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Portrait"
android:textSize="50sp"
android:textColor="#ffffffff"/>
</LinearLayout>
AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mytest.ActivityMain"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您可以在清單中沒有「android:configChanges」屬性的情況下試用嗎? –