2017-06-13 40 views
1

我試圖實現垂直分隔屏幕40/60的佈局,使用layout_weight。Android(Xamarin)linearlayout佈局:重量沒有按預期工作

下面是XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="1" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:background="@drawable/background"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4"> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/white" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.6"> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/black" /> 
    </LinearLayout> 
</LinearLayout> 

此代碼不能正常工作,返回一個空白屏幕。但是,如果我改變

的android:layout_width = 「FILL_PARENT」

機器人:layout_height = 「0dp」

機器人:layout_weight = 「0.4」

(垂直分割)

機器人:layout_width = 「0dp」

機器人:layout_height = 「FILL_PARENT」

機器人:layout_weight = 「0.4」

(水平分割)

代碼按預期工作並且佈局水平分割40/60。爲什麼它不能垂直工作,我該如何解決這個問題?任何幫助,將不勝感激!

+1

給予父佈局方向垂直 – Anmol

+0

@Anmol奇怪,敢發誓我試過了,並沒有工作...謝謝雖然!另外,您是否介意回答問題而不是回覆,以便我可以將此帖標記爲已回答? – Ryalis

+0

當然,歡迎 – Anmol

回答

2

給予父佈局方向垂直

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:weightSum="1" 
android:focusable="true" 
android:orientation="verticle" 
android:focusableInTouchMode="true" 
android:background="@drawable/background"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.4"> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/white" /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.6"> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/black" /> 
</LinearLayout>