2011-12-12 29 views
2

我對我的按鈕有點問題。我想在(大約)1/3和我的屏幕的2/3,讓他們(我下面提供的截圖,以使事情更清晰) 我的代碼如下:如何在屏幕的某些位置對齊視圖,在多個屏幕分辨率下保持一致?

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

    <Button 
     android:text="@string/topbutton" 
     android:background="@drawable/silvertablesbuttonbgkort" 
     android:id="@+id/topbutton" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center|center_vertical"> 
    </Button> 

    <Button 
     android:text="@string/midbutton" 
     android:background="@drawable/silvertablesbuttonbglang" 
     android:id="@+id/midbutton" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center|center_vertical"> 
    </Button> 


<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

我試圖讓它看起來像這樣:(我很抱歉,發佈屏幕截圖的模型在這裏是皺眉,我只是想不出一個更好的方式來清理我想要做的)

看來,我不能發佈圖片,因爲我太新了在這裏...所以這裏是一個鏈接截圖:http://i.imgur.com/l3b3z.png

我最初的想法是在兩個按鈕周圍環繞一個垂直線性佈局,並在兩者之間放置空的文本視圖,如果此應用旨在在一個屏幕大小上運行,實際上它將工作,但我很漂亮確定它會在另一個屏幕分辨率的每個手機上搞砸。我通過使用android:layout_alignParentBottom =「true」的相關佈局解決了底部酒吧的這個問題,但我真的無法想到一種方法來捕捉1/3或2/3。有沒有辦法做到這一點,將與各種屏幕分辨率工作?

編輯: 我解決了我的問題!我試圖將其作爲答案張貼,但我不能在8小時之內。這樣做,但現在,我只是將它作爲編輯發佈在這裏:

我已經在屏幕中間放置了一個0x0的TextView,並將RelativeLayouts放在它的上面和下面,填充屏幕。然後,我在這些佈局的中間放置了兩個TextView,它們的佈局中有兩個新的RelativeLayouts。一個低於最高TextView,一個高於最低。我把我的按鈕放在這些佈局的中心。它像魅力一樣工作,除了代碼本身之外不依賴任何東西。這是我現在的代碼:

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


    <TextView 
     android:text=" " 
     android:id="@+id/ankermidden" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

    <RelativeLayout 
     android:id="@+id/ankerveldboven" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/ankermidden"> 

       <TextView 
        android:text=" " 
        android:id="@+id/ankerboven" 
        android:layout_centerVertical="true" 
        android:layout_width="0dp" 
        android:layout_height="0dp"> 
       </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidboven" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/ankerboven"> 

       <Button 
        android:text="@string/topbutton" 
        android:background="@drawable/silvertablesbuttonbgkort" 
        android:id="@+id/topbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout> 

</RelativeLayout> 


<RelativeLayout 
     android:id="@+id/ankerveldonder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/ankermidden"> 

    <TextView 
     android:text=" " 
     android:id="@+id/ankeronder" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidonder" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@id/ankeronder">  

       <Button 
        android:text="@string/midbutton" 
        android:background="@drawable/silvertablesbuttonbglang" 
        android:id="@+id/midbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout>  

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

所以是的,這是比我想象的更容易。

回答

1

我已經找到了解決方案!一個完全相對於自身,並且不依賴像素或密度像素。

我已經在屏幕中間放置了一個0x0的TextView,並將RelativeLayouts放在它的頂部和底部,填滿了屏幕。

然後,我在這些佈局的中間放置了兩個TextView,它們是佈局中的兩個新的RelativeLayouts。一個低於最高TextView,一個高於最低。我把我的按鈕放在這些佈局的中心。

它像一個魅力一樣工作,並不依賴於任何東西,而不是代碼本身。

這是我現在的代碼:

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


    <TextView 
     android:text=" " 
     android:id="@+id/ankermidden" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

    <RelativeLayout 
     android:id="@+id/ankerveldboven" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/ankermidden"> 

       <TextView 
        android:text=" " 
        android:id="@+id/ankerboven" 
        android:layout_centerVertical="true" 
        android:layout_width="0dp" 
        android:layout_height="0dp"> 
       </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidboven" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/ankerboven"> 

       <Button 
        android:text="@string/topbutton" 
        android:background="@drawable/silvertablesbuttonbgkort" 
        android:id="@+id/topbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout> 

</RelativeLayout> 


<RelativeLayout 
     android:id="@+id/ankerveldonder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/ankermidden"> 

    <TextView 
     android:text=" " 
     android:id="@+id/ankeronder" 
     android:layout_centerVertical="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"> 
    </TextView> 

      <RelativeLayout 
       android:id="@+id/ankerveldmidonder" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@id/ankeronder">  

       <Button 
        android:text="@string/midbutton" 
        android:background="@drawable/silvertablesbuttonbglang" 
        android:id="@+id/midbutton" 
        android:layout_alignParentRight="true" 
        android:layout_centerInParent="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center|center_vertical"> 
       </Button> 

      </RelativeLayout>  

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/underbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 

    <Button 
     android:text="@string/underbartekst" 
     android:background="@drawable/silvertablesunderbar" 
     android:id="@+id/underbarbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:textSize="20dp" 
     android:gravity="left|center_vertical" > 
    </Button> 

</RelativeLayout> 

所以是的,這是比我想象的更容易。

Bill Gary建議在dp中使用一個邊距,以保持不同屏幕尺寸上的相同比例,但是經過大量實驗後,事情在不同的屏幕上再次顯得很怪異。

在我回到這個之前,我會做一些更多的嘗試,因爲這個整體的傾斜邊緣的東西正在讓我失望......應該正確顯示的東西不是,而且不應該的東西,在一些屏幕分辨率上做。

我會開始做這些東西的作業,但現在雖然有點長,但我上面發佈的代碼對我來說完美無瑕。

0

試試這個,你可能需要調整的DP機器人:layout_marginTop = 「150dp」

<Button 
    android:text="@string/topbutton" 
    android:background="@drawable/silvertablesbuttonbgkort" 
    android:id="@+id/topbutton" 
    android:layout_alignParentRight="true" 
    android:layout_marginTop="150dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center|center_vertical"> 
</Button> 
+0

這是一種可能性,但仍然會在小型和更具異國情調的屏幕上出現問題(例如:銀河系Pro)會不會?因爲那是我第一次寫的東西,現在整個事情都是相對的,但我不能在8小時內將它作爲答案發布,因爲我是新來的。如果您有興趣,我會發布我的文章作爲對我的文章的修改。 – Louis

+0

嗯,似乎無法編輯第二次發佈最後一篇文章...我現在覺得有點愚蠢,我對dps的工作方式有一個完全錯誤的想法。開始懷疑自己並使用Google搜索,結果發現這是一件聰明的事情。我認爲dps允許人們指定與屏幕實際大小相關的事物。是的,我知道,笑聲和麪部表情都允許在這裏,但至少你已經瞭解了我現在編程的新內容。非常感謝您的回答。 – Louis

+0

別擔心,我是新人,仍然在學習 –