2015-11-07 112 views
0

我試過並搜索了一段時間,將片段的相對佈局劃分爲兩半,但沒有成功。最後,我設法將它們分開,但是,圖像並沒有填充屏幕的一半,因此當我嘗試按照圖像中顯示的文字居中時,出現一些錯誤,所以最後我爲了運行應用程序,必須清除文本的居中。我把一個看不見的物體放在屏幕的中央,以便將它分成兩半,但我沒有得到預期的結果。如果有人能幫我解決這個問題,我會很感激。在上半部分(頂部),我想要有一個圖像,在它下面,另一半,一個文本應該如圖所示居中:將RelativeLayout分成一半(圖像和文字)

Divided 。這裏是我的片段:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View android:id="@+id/fakeView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true"/> 

    <TextView 
     android:id="@+id/population" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/fakeView" 
     android:layout_alignParentBottom="true" 
     /> 

    <ImageView 
     android:id="@+id/flag" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/fakeView" 
     android:layout_alignParentTop="true" 
     /> 



</RelativeLayout> 
+0

爲什麼這必須是'RelativeLayout'?爲什麼不在'LinearLayout'中設置兩半權重相等? – CommonsWare

+0

事情是,我也曾嘗試使用LinearLayout,但我沒有得到預期的結果。即使使用fill_parent,圖像也沒有填充屏幕的一半,我無法使文本按照指定居中。所以,我想也許有人會知道RelativeLayout的方式,因爲它更平滑,而且會更好。當然,如果我無法使用RelativeLayout工作,LinearLayout也可以工作。 –

+0

「因爲它更平滑,它會更好」 - 我不知道這意味着什麼。將'LinearLayout'分成兩半非常簡單,參見[this layout](https://github.com/commonsguy/cw-omnibus/blob/master/ConfigChange/Bundle/app/src/main/res/layout/main .xml)爲例。 – CommonsWare

回答

1

嘗試在兩個視圖上使用LinearLayout和layout_weight。爲他們設置相同的值。並且不要忘記將layout_height設置爲0才能使其工作。