2015-04-08 50 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