2012-07-29 61 views
-1

好吧,這個問題已經徘徊了我大約2個星期了,我已經完成了所有的研究,我仍然無法弄清楚這一點。我有一個餅圖形式的6個按鈕。當我在XML中進行佈局時,我可以讓它們看起來很好。我如何讓按鈕留在一個地方在android

view I want to achieve

當我將其更改爲平板電腦的尺寸或其他尺寸的屏幕上我所有的按鈕移動,慘不忍睹。

bad view

如何使這些按鈕在任何屏幕尺寸固定的?我知道我必須在適當的文件夾中擁有不同的圖像大小,並且我打算這麼做,但我需要知道是否有辦法將這些文件鎖定在XML文件中的某個位置或我需要做什麼使這項工作正常。和往常一樣,任何和所有的幫助都非常感謝,如果我對我的問題不夠清楚,我會回答任何問題。感謝

這裏是我的XML

<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:background="@drawable/mainback" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="25dp" 
    android:background="@drawable/ipadcollegesm" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_alignRight="@+id/button1" 
    android:layout_marginBottom="92dp" 
    android:background="@drawable/ipadmusicsm" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button2" 
    android:layout_marginLeft="5dp" 
    android:layout_toRightOf="@+id/button1" 
    android:background="@drawable/ipadfamilysm" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button3" 
    android:layout_alignTop="@+id/button1" 
    android:background="@drawable/ipadyouthsm" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button4" 
    android:layout_below="@+id/button3" 
    android:layout_marginTop="14dp" 
    android:background="@drawable/ipadlinkssm" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button5" 
    android:layout_alignBottom="@+id/button5" 
    android:layout_toLeftOf="@+id/button4" 
    android:background="@drawable/ipadpodcastsm" /> 

</RelativeLayout> 
+0

發佈您的XML佈局文件.. – tpow 2012-07-29 03:55:42

+0

你去了,忘了這麼做,並且正如你所評論的那樣做。我試圖做一個框架佈局與嵌套在其中的相對佈局,這只是沒有工作正確的,所以這是我想要做的基本前提 – Bryan 2012-07-29 03:58:05

回答

2

你提供一個固定的DP爲每個按鈕,不同的設備有不同的分辨率。

爲了使這個工作在最少工作量的多個設備上,我建議添加另一個相對佈局作爲根,並將重力設置爲中心。這樣,佈局總是在屏幕的中心。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:background="@drawable/mainback"> 
    <RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

     *** Your buttons would be here *** 

    </RelativeLayout> 
</RelativeLayout> 

另一種方法是每個屏幕尺寸都有一個dimens文件,但上面可能是最簡單的方法。

+0

我會改變按鈕上的任何東西或只是讓他們的方式我有他們,只是將它們嵌套在第二個相對佈局 – Bryan 2012-07-29 04:22:25

+0

我會認爲他們應該沒問題,但正如學者賈斯汀比伯曾經說過的「永不說永不」。也許增加一個FrameLayout或者一個保存中心的東西,然後用它作爲錨點會對你更有利。 – jimmithy 2012-07-29 04:33:04

相關問題