2013-12-23 520 views
2

我製作了一個假人View並將其填充重量= 1爲什麼我的android:layout_weight不起作用?

我希望所有額外的空間都可以進入該視圖,但它在現實中不會發生。

我錯過了什麼?

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

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <com.m.view.text.MyTextView 
        android:id="@+id/whyResgisterHeaderText" 
        style="@style/textOnBg" 
        android:layout_marginBottom="10dp" 
        android:layout_marginTop="30dp" 
        android:text="WHY REGISTER?" 
        android:textAllCaps="true" 
        android:textColor="@android:color/white" 
        app:font_type="varela" /> 

       <com.m.view.text.MyTextView 
        android:id="@+id/whyResgisterBodyText" 
        style="@style/textOnBg" 
        android:lineSpacingMultiplier="1.2" 
        android:text="first line\nsecond line\nthird line\nforth line" 
        android:textStyle="italic" /> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dp" 
        android:layout_marginTop="20dp" 
        android:src="@drawable/signup_illu_why" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" /> 

       <LinearLayout 
        android:id="@+id/gotItAction" 
        android:layout_width="283dp" 
        android:layout_height="52dp" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp" 
        android:layout_marginTop="10dp" 
        android:background="@drawable/btn_selector" 
        android:clickable="true" 
        android:gravity="center" 
        android:orientation="horizontal"> 

        <com.m.view.text.MyTextView 
         android:id="@+id/goItText" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:gravity="center" 
         android:text="Got it" 
         android:textColor="#00bcfe" 
         android:textSize="16dp" 
         android:textStyle="italic" /> 
       </LinearLayout> 
      </LinearLayout> 
     </ScrollView> 
    </RelativeLayout> 
+0

重量不相對佈局 –

回答

0

我想你如果你正在使用類似LinearLayout父元素,並使用layout_weight裏面在你的第一個LinearLayout中添加android:weightSum="1.0"(其中包含的觀點具有重量的)

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="1.0" 
      android:orientation="vertical" > 
+0

內工作,它沒有用是這樣的。我會嘗試 –

+0

不。相同的結果 –

0

他們的child element你可能會得到期望的結果。但是,如果您有ScrollView作爲主要父項,並且在其child element之內使用layout_weight屬性,則可能不會證明這是一個好結果。 Bcs,ScrollView是無止境的,它只會在其子元素完成的時候結束。所以,很明顯如果你使用weight屬性,那麼Android可能無法找出你想要結束scroll的地方並且行爲不當。

因此,儘量保留LinearLayout其中包含TextView(與Got It文本)在主佈局的底部。將LinearLayout刪除並將其粘貼在ScrollView之外,以便它將在滾動的底部。

試試這個..

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

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <com.m.view.text.MyTextView 
       android:id="@+id/whyResgisterHeaderText" 
       android:layout_marginBottom="10dp" 
       android:layout_marginTop="30dp" 
       android:text="WHY REGISTER?" 
       style="@style/textOnBg" 
       android:textAllCaps="true" 
       android:textColor="@android:color/white" 
       app:font_type="varela"/> 

      <com.m.view.text.MyTextView 
       android:id="@+id/whyResgisterBodyText" 
       style="@style/textOnBg" 
       android:lineSpacingMultiplier="1.2" 
       android:text="first line\nsecond line\nthird line\nforth line" 
       android:textStyle="italic" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="20dp" 
       android:layout_marginTop="20dp" 
       android:src="@drawable/signup_illu_why" /> 
     </LinearLayout> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/gotItAction" 
     android:layout_width="283dp" 
     android:layout_height="52dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerInParent="true" 
     android:layout_marginBottom="20dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/btn_selector" 
     android:clickable="true" 
     android:gravity="center" 
     android:orientation="horizontal" > 

     <com.m.view.text.MyTextView 
      android:id="@+id/goItText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:text="Got it" 
      android:textColor="#00bcfe" 
      android:textSize="16dp" 
      android:textStyle="italic" /> 
    </LinearLayout> 

</RelativeLayout> 
+0

你做了什麼? –

+0

@EladBenda檢查我的編輯的解釋.. – Hariharan