2014-02-26 29 views
-1

我的裸機測試應用程序將啓動時的方向設置爲橫向。它有一個佈局和佈局端口目錄。每個目錄中都有一個簡單的佈局文件。它們包含一個文本視圖,其文本設置爲「橫向」或「縱向」。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> 
+0

您可以在清單中沒有「android:configChanges」屬性的情況下試用嗎? –

回答

1

變化/res/layout/my_layout.xml/res/layout-land/my_layout.xml
注:我添加後綴-land

+0

謝謝!我其實早些時候嘗試過。但它不會改變結果。 –

0

你不得不刪除選項「方向「清單中:

android:configChanges="screenSize|orientation|keyboardHidden" 

因爲這個選項是說你自己處理吧...

Good explanation here: