2010-12-07 22 views
3

搜索了幾個小時後,我無法找到確切的答案,我的情況。我目前正在使用RelativeLayout,我擁有的是背景圖片和按鈕。我遇到的問題是將按鈕放在我想要的位置(與中心稍微偏移)。如何設置圖像或按鈕的絕對位置,使其在多個屏幕尺寸上保持一致?

我的第一次嘗試是修改按鈕的layout_margins。雖然這適用於當前我正在使用(3.7英寸WVGA)模擬器的仿真器,但按鈕的位置對於不同的屏幕尺寸(例如3.2英寸HVGA)略微/關閉。

然後我試圖修改父佈局的填充,但得到同樣的問題。我的XML看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background"> 
    <Button android:id="@+id/starttimer" 
      android:background="@drawable/button" 
      android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> 
</RelativeLayout> 

請讓我知道,如果我完全沒有我的方法。這看起來像是一個非常簡單的問題,我很失望,它花了我很長時間纔得到它。謝謝你的幫助!

+0

您是否嘗試過絕對佈局? – Sandy 2010-12-07 08:48:02

+1

不是,僅僅因爲我讀了它不推薦。如果我錯了,請糾正我。 – marcuspfister 2010-12-07 08:49:38

回答

2

我認爲你想要在父母的底部居中放置一個小偏移量的按鈕,請嘗試layout_centerHorizontal然後添加你的首選邊距。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:paddingLeft="98dip" android:paddingBottom="68dip" android:background="@drawable/background"> 
    <Button android:id="@+id/starttimer" 
      android:background="@drawable/button" 
      android:layout_width="wrap_content" android:clickable="true" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android_marginLeft="5dp"/> 
</RelativeLayout> 
+0

Andreas&Sandy - 我很欣賞超級快速的迴應。不幸的是,問題似乎仍然存在,即不同屏幕尺寸的位置正在發生變化。另外,在添加layout_centerHorizo​​ntal屬性後,我的水平邊距不再生效。 – marcuspfister 2010-12-07 08:57:54

0

絕對佈局用於控件的絕對位置。

相關問題