2017-01-11 36 views
-2

layout_weight內的佈局不起作用layout_weight不起作用,在設計視圖中顯示正確,但編譯時出錯並停止編譯 我的代碼是當相對佈局中的Linearlayout(只有問題出現在layout_weight中)時,相對佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/black" 
     android:id="@+id/ll_bookmarkslistad" 
     android:weightSum="100" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="30" /> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="70" 
      android:layout_marginRight="0.0dp" /> 
    </LinearLayout> 
</RelativeLayout> 
+0

歡迎來到堆棧溢出!爲了幫助人們回答您的問題,您需要更加具體地瞭解錯誤。請[編輯]您的帖子以合併您從[mcve]中獲得的確切錯誤(最好使用複製+粘貼以避免轉錄錯誤)。 –

+0

第一件事,在你的xml中你沒有添加線性佈局的方向,第二件事是你已經添加了線寬佈局的權重,那麼你必須將子視圖的高度或寬度設置爲0dp以及所需的重量對於視圖(你已經在你的xml中指定了它)。現在我的問題是你想實現什麼?你想要在屏幕中心的兩個按鈕? –

+0

和我的建議是,而不是使用填充家長使用匹配的父母,因爲他們都做同樣的工作,並填寫家長在棄用, –

回答

0

對於遲到的回覆感到抱歉。它會幫助你。 :)

<?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"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@android:color/darker_gray" 
     android:layout_centerInParent="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" hello world" 
      android:layout_weight="3"/> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="7"></Spinner> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@android:color/darker_gray" 
     android:layout_alignParentBottom="true"> 
     <Button 
      android:id="@+id/btn1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="btn1" 
      android:layout_weight="3" 
      /> 
     <Button 
      android:id="@+id/btn2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="btn2" 
      android:layout_weight="4" 
      /> 
     <Button 
      android:id="@+id/btn3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="btn3" 
      android:layout_weight="3" 
      /> 
    </LinearLayout> 
</RelativeLayout> 
+0

謝謝你的回覆...我已經完成了你的第一個回覆,並提出了很好的建議.....並且我認爲在這個代碼方向上無法在相對佈局 – shake

+0

歡迎:) .yup。方向僅適用於線性佈局。您無法將方向屬性設置爲相對佈局。 –