2012-04-13 30 views
0

我只知道這是簡單,大約30分鐘的時間,我會恨我自己......定位與頂視圖表示爲顯示高度的百分比

我有一個由靜態圖像的啓動畫面填滿屏幕。所以我簡單地設置了我在佈局中使用的任何根視圖的背景屬性。

圖像有一個空白區域,我需要放置一個「我接受」按鈕。要處理不同的解決方案,我必須使用顯示高度的百分比來定位 - 58%是現場。

我不能使用layout_weight,因爲這樣會調整button和absolutelayout(在代碼中設置y位置)的大小。

我該如何做到這一點?我不在乎哪個視圖組是父視圖,我對填充空間的「空白」視圖沒有問題。

我的目標在佈局XML完全這樣做是爲了保持我的代碼清潔...

謝謝!

回答

4

你說你不能使用layout_weight,但這是你唯一的選擇,如果你想純粹用XML。我不明白你爲什麼認爲你無法使用它。這裏有一個如何做到這一點的例子:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/splash" 
    android:orientation="vertical" > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="58" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="42" > 

      <!-- Place buttons here --> 

    </LinearLayout> 
</LinearLayout> 
+0

我認爲西蒙關注的是應用'layout_weight'其高度的'Button'本身會迫使尺寸屏幕的42%。按照您的建議將權重應用於'LinearLayout'顯然可以防止這種情況發生,並允許'Button'採用其'自然'大小。 – Squonk 2012-04-13 16:00:30

+0

謝謝傑森。正如預期的那樣,一個「doh」時刻:)這是一個冠軍...... – Simon 2012-04-14 06:49:05

0

我沒有看到使用layout_weight ......還有全班AbsoluteLayout已過時,因此要儘量避免使用它的任何其他方式。我建議使用LinearLayout作爲你的rootView,給定的weight_sum爲1.添加另一個空間填充的LinearLayout寬度,權重爲0.58,並且在你的Button下使用wrap_content屬性。不幸的是,我不能告訴你更多,除非你發佈你的XML,以便我可以看到,你試圖達到什麼目的。 類這應該工作:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/your_desired_background" 
    android:orientation="vertical" 
    android:weight_sum="1"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".58" /> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout>