2012-03-09 196 views
6

我有兩個活動,根據SD卡的存在情況打開。一個活動有三個Buttons和一個TextView和另一個ImageView,一個按鈕和縮放控制。當我改變方向時,這些按鈕會在水平方向亂翻。如何解決這個問題?更改方向時更改Xml佈局

我的「SD」卡布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#1E1E1E" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button_print" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="44dp" 
     android:background="@drawable/my_button" 
     android:text="@string/print" /> 

    <TextView 
     android:id="@+id/text_SDmissing" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="80dp" 
     android:text="@string/SDmissing" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/button_camera" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button_print" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="58dp" 
     android:background="@drawable/my_button" 
     android:text="@string/camera" /> 

    <Button 
     android:id="@+id/button_insert" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text_SDmissing" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="40dp" 
     android:background="@drawable/my_button" 
     android:text="@string/insert" /> 

</RelativeLayout> 
+0

請告訴我們您的佈局XML文件(S)。 – 2012-03-09 08:30:26

+1

請發佈您的xml文件。如果您需要根據方向更改佈局,則必須爲layout-port和layout-land設置兩個佈局。 – deepa 2012-03-09 08:31:56

+1

您是否嘗試過爲縱向/橫向提供不同的佈局? – Luksprog 2012-03-09 08:32:30

回答

8

您可以在您的項目稱爲「佈局 - 土地」的文件夾(在「資源」目錄)。在那裏,你可以從正常的「佈局」文件夾複製所有的XML樣式,並重新設置它們爲橫向模式。 Android會自動將這些佈局用於橫向模式。

您的項目應該是這樣的:

enter image description here

13

你必須創造一個縱向和橫向模式單獨的XML文件,並將其放置在不同的目錄中。該設備將自動選擇正確的一個。您可以使用下面的目錄結構

res/layout/my_layout.xml 
res/layout-land/layout.xml 

有關進一步的參考,您可以檢查: http://developer.android.com/guide/practices/screens_support.html

+0

罰款:)我的另一個問題是如何使用佈局來支持不同的屏幕大小.....我所做的是,每次我重新排列和運行應用程序,並檢查我的設備,但它不一樣,如果我在不同的應用程序中運行相同的程序..... – jxgn 2012-03-09 11:22:17

+0

兩個文件名都必須相同。 – 2016-04-20 06:55:43