2013-10-29 42 views
8

嘿,我從屏幕頂部到屏幕底部之前都有一個滾動視圖,因爲這是我的廣告橫幅發生的地方。由於谷歌政策,我希望在這些元素(按鈕和橫幅之間)之間有一些像素。到目前爲止,我在dp中設置了一個值。但是,由於設備的不同,這會導致廣告和某些高分辨率手機上的滾動視圖之間存在巨大差距。這就是爲什麼我想設置Android獲取XML格式的屏幕大小

<ScrollView 
android:layout_height="(Screen_height-banner)-5px" 

(當然,我甚至沒有嘗試這種方式,因爲這是沒有代碼,但我認爲它總結了相當不錯我打算做)

非常感謝您的回答!

回答

2

您不能查詢XML中的屏幕尺寸,因爲它在運行時不運行,您可以通過使用RelativeLayout做到這一點,使用alignmentsmargins

你的情況

如果SCREEN_HEIGHT橫幅代表了屏幕高度,你想它在底部位置,並從年底5DP分離器的XML將

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

     <ScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_marginBottom="5dp"> 

</RelativeLayout > 

如果您需要縮放滾動視圖,而不是的位置它你xml將是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding_bottom="5dp"> 

     <ScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent"> 

</RelativeLayout > 
+0

API API Level 8及更高版本將「fill_parent」替換爲「match_parent」 – Antonin

1

使用的RelativeLayout,設置5px的一個保證金和使用layout_above

2

你可以用戶權重參數。如果您將兩個佈局的權重設置爲0.5,則這些佈局將共享屏幕寬度等於。

<LinearLayout 
    android:id="@+id/alt_panel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/orta_panel" > 

    <LinearLayout 
     android:id="@+id/altsol_panel" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 


    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/altsag_panel" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.5" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 


    </LinearLayout> 
</LinearLayout>