2013-02-12 141 views
2

我有一個菜單活動,其中有幾個定位按鈕。它在縱向模式下完美適合我的手機屏幕,但在橫向模式下它會被破壞。我知道,當我有固定的位置時,這將是結果。任何人都可以告訴我如何定位我的按鈕,所以我的活動也保持其形式在橫向模式?謝謝。Android:從縱向轉向橫向時定位按鈕

這裏是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:orientation="vertical" > 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/bezpecnost_over" 
    android:layout_marginLeft="30dp" 
    android:layout_marginTop="50dp" 


     /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/kurenie_over" 
    android:layout_marginLeft="200dp" 
    android:layout_marginTop="50dp" 

    /> 

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/osvetlenie_over" 
    android:layout_marginLeft="200dp" 
    android:layout_marginTop="200dp" 
    android:onClick="obrOsv" /> 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:background="@drawable/pohodlie_over" 
    android:layout_marginLeft="30dp" 
    android:layout_marginTop="200dp" 
    /> 

</RelativeLayout> 

回答

3

它非常適合我的手機屏幕處於縱向模式時,但在 橫向模式下它的破壞。

這是基於相對佈局的預期結果。通常做在這種情況下什麼開發商:

  • ,務必資源稱爲layout-land
  • 的文件夾進行帶相同名在縱向模式佈局的XML(包含在res/layout
  • 位置景觀模式。

由於XML文件名是相同的,當它是時間的活動來設置內容視圖

setContentView (R.layout.my_layout); 

layout-land文件夾中的文件my_layout.xml將被代替使用。

而且讀取從Android文檔Layouts & Supporting Multiple Screens,它們都具有與後者談論不同限定符(如layout-land)的有用信息。

+1

這就是我教如何做到這一點的方式。除非你想根據屏幕尺寸縮放他們的位置?不要以爲這樣做或者對同一個解決方案來說工作太多。 – 2013-02-12 22:24:17

+0

那麼你有什麼建議? – 2013-02-13 20:17:15

+0

@MartinNemeth [this](http://developer.android.com/training/multiscreen/screensizes.html)鏈接應該更深入地討論如何支持多個screen *大小*。它討論瞭如何使用match_parent來獲得優勢以及如何使用限定符。但請記住,佈局越簡單,適應多個屏幕越容易。 – 2013-02-13 20:45:57

0

因此,您正在使用相對佈局來通過使用增加的邊距來水平間隔按鈕?這完全不理解佈局如何工作。首先將RelativeLayout替換爲方向設置爲水平的LinearLayout,並刪除所有邊距 - 邊距應該僅爲元素之間的幾個像素。我建議你閱讀關於佈局如何工作的Android教程。

相關問題