2011-07-15 214 views
1

我正在使用RelativeLayout,並且想要將中心的橙色中心放在中間並填充90%的寬度。我知道這可以使用LinearLayout使用百分比,但我如何使用RelativeLayout來做到這一點?Android:在RelativeLayout中設置視圖的寬度百分比

我是否必須以編程方式計算屏幕的寬度(以dp爲單位),並將這個90%設置爲橙色視圖的寬度?

總之,我想要這樣的東西,其中橙色條使用RelativeLayout佔用屏幕中心的90%,而不用硬編碼x和y座標,以便它可以在所有屏幕密度上工作。 (在邊緣的暗條是手機的邊緣)

enter image description here

回答

1

只是一個猜測,但值得一試:) 請重新設計你的佈局結構如下圖所示:

<LinearLayout[with 90% width]> 
    <RelativeLayout 
      android:layout_width="fill_parent" 
      ... 
     <!--this will fill up its parent i.e above LinearLayout which is 90% of the screen width --> 
    > 
      <!--child views goes here.. --> 
    </RelativeLayout> 
</LinearLayout> 
+0

實際上,這是我一直在尋找。我是新手! –

0

使用新的百分比支持庫

compile 'com.android.support:percent:24.0.0' 

見下文例如

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     app:layout_widthPercent="50%" 
     app:layout_heightPercent="50%" 
     app:layout_marginTopPercent="25%" 
     app:layout_marginLeftPercent="25%"/> 
</android.support.percent.PercentRelativeLayout> 

*更多信息* Tutorial1Tutorial2Tutorial3

相關問題