2015-04-08 51 views
0

我有一個ScrollView和兩個LinearLayouts的片段。我希望能夠將第一個LinearLayout放在頂部(重力頂部),第二個放在底部。這裏是我的代碼在ScrollView中設置重力

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" 
     tools:context=".activities.NavigationDrawerFragment" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:background="@color/darkblue" 
      android:paddingBottom="40dp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:paddingTop="20dp" 
      android:layout_gravity="center|top"> 

      <ImageView 
       android:id="@+id/img_drawler_value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher" 
       /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:layout_marginLeft="5dp"> 
       <TextView 
        android:id="@+id/txt_drawler_name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/txt_drawler_name" 
        android:textColor="@android:color/white" 
        android:textStyle="bold" 
        android:textSize="20sp" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        > 
        <TextView 
         android:id="@+id/txt_drawler_time_avariable" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/txt_drawler_time_avariable" 
         android:textColor="@android:color/white" 
         android:textSize="12sp" 
         android:textIsSelectable="false" /> 
        <TextView 
         android:id="@+id/txt_drawler_time_value" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/empty" 
         android:textSize="12sp" 
         android:textColor="@android:color/white"/> 
       </LinearLayout> 
      </LinearLayout> 


     </LinearLayout> 

     <ScrollView 
      android:scrollbars="none" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fillViewport="true" 
      android:id="@+id/id_drawable_scrollView"> 

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

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

         <Button 
          android:id="@+id/btn_drawler_home" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:background="@android:color/transparent" 
          android:gravity="center|left" 
          android:paddingBottom="10dp" 
          android:paddingLeft="10dp" 
          android:paddingTop="10dp" 
          android:text="@string/btn_drawler_home" 
          android:textColor="@android:color/black" 
          android:textSize="16sp" /> 

         <View 
          android:layout_width="fill_parent" 
          android:layout_height="1dp" 
          android:background="@color/lightgray" /> 

         <Button 
          android:id="@+id/btn_drawler_select" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/btn_drawler_class_select" 
          android:textColor="@android:color/black" 
          android:background="@android:color/transparent" 
          android:gravity="center|left" 
          android:textSize="16sp" 
          android:paddingTop="10dp" 
          android:paddingBottom="10dp" 
          android:paddingLeft="10dp" /> 

         <View 
          android:layout_width="fill_parent" 
          android:layout_height="1dp" 
          android:background="@color/lightgray"/> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:background="@color/green" 
        android:paddingBottom="20dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 
        android:paddingTop="30dp" 
        android:gravity="bottom"> 

        <TextView 
         android:id="@+id/btn_drawler_toc" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/transparent" 
         android:text="@string/btn_drawler_toc" 
         android:gravity="left|center" 
         android:textColor="@android:color/white" 
         android:textSize="16sp" 
         android:paddingBottom="20dp" 
         /> 

        <TextView 
         android:id="@+id/btn_drawler_faq" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/transparent" 
         android:text="@string/btn_drawler_faq" 
         android:gravity="left|center" 
         android:textColor="@android:color/white" 
         android:textSize="16sp" 
         android:paddingBottom="20dp" 
         /> 

        <TextView 
         android:id="@+id/btn_drawler_contact" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/transparent" 
         android:text="@string/btn_drawler_contact" 
         android:gravity="left|center" 
         android:textColor="@android:color/white" 
         android:textSize="16sp" 
         android:paddingBottom="20dp" 
         /> 

        <TextView 
         android:id="@+id/btn_drawler_logout" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/transparent" 
         android:text="@string/btn_drawler_logout" 
         android:gravity="left|center" 
         android:textColor="@android:color/white" 
         android:textSize="16sp" 
         /> 

       </LinearLayout> 
      </LinearLayout> 
     </ScrollView> 

    </LinearLayout> 

似乎ScrollView的孩子不能fill_parent或match_parent。這可能嗎?

我想實現

enter image description here

謝謝

更新1

我不知道爲什麼不利用這種結構的工作,在這最後的情況下滾動什麼不工作。

<RelativeLayout> 
    <ScrollView> 
     <LinearLayout> 
      <LinearLayout> 
      </LinearLayoyt> 
      <LinearLayout> 
      </LinearLayoyt> 
     </LinearLayoyt> 
    </ScrollView> 
    <LinearLayout> 
    </LinearLayoyt> 
</RelativeLayout> 

順便說一句,我覺得這對我的工作(使用不同的XML佈局)的設置,但我不知道爲什麼不工作,也許有人能幫助我理解。

+0

放圖像的頂部LinearLayout android:layout_alignParentBottom="true",我不明白你想要什麼,什麼是目前的狀況 –

+1

這個問題讓沒有意義。 「ScrollView」可能具有無限高度,具體取決於其包含的視圖數量(及其組合高度)。 「ScrollView」的「頂部」或「底部」的概念只有在創建並填充了各種視圖後纔有效。將一些東西放在ScrollView的「頂部」和其他位於「底部」的東西(大概在中間存在某種空隙)並不是ScrollView的工作原理。 – Squonk

+0

謝謝@Squonk。你幫助我理解了發生的更好,但是我的問題是如果ScrollView中的元素小於「我有的高度」ScrollView在底部填充了白色背景,我更喜歡中間的填充,在作爲ScrollView子項的LinearLayout子項的兩個LinearLayout之間。 – agusgambina

回答

1

而不是使用LinearLayout,只需使用RelativeLayout(作爲ScrollView中的包裝)。和然後重力而頂部和底部,使用:

android:layout_alignParentTop="true"底部LinearLayout

+0

謝謝,我幾乎在那裏,你的答案我能夠設置我想要的顯示。我使用兩個RelativeLayouts一個用於整個視圖,另一個用於ScrollView內的RelativeLayout來設置底部的LinearLayout。問題是,如果這兩個元素重疊ScrollView不起作用,我不能拖動屏幕。 – agusgambina

+0

謝謝,不是解決方法,而是最好的答案。最後,我最終使用了這個答案併爲不同的屏幕定義了佈局。這就是爲什麼我會將這個答案標記爲正確的。 – agusgambina

+0

@agusgambina謝謝,但爲了充分幫助你,我需要知道你的佈局目標是什麼。 –

0

您想要在視圖底部的線性佈局是在另一個具有「中心」重力的線性佈局下,因此第二個線性佈局將位於父佈局的底部 試圖使第二個佈局作爲父級而不是另一個佈局的子級

+0

謝謝你的回答。我試過但沒有工作。 – agusgambina