2014-10-06 213 views
0

我有一个包含一个子布局(线性布局)的滚动视图,并且该子包含相对布局和线性布局。相对布局和线性布局共享半屏。线性布局具有动态添加的按钮。ScrollView不滚动溢出布局

现在问题出现了,因为滚动视图在滚动相对布局和线性布局时不滚动按钮。

<ScrollView> 
<LinearLayout> 

    <RelativeLayout>  
    </RelativeLayout> 

    <LinearLayout> 
          ->Dynamically adding buttons here 
    </LinearLayout> 

</LinearLayout> 
</ScrollView> 

我想这些按钮与滚动视图滚动

仅供参考我的XML代码: -

<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="@color/whitecolor" 
android:orientation="vertical" 
> 

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    > 

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

<RelativeLayout 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    > 
<View 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/bluecolor" 
    /> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/stauslinearlayout" 
    android:layout_marginBottom="8dp" 
    android:padding="5dp" 
    android:gravity="center" 
    android:textColor="@color/whitecolor" 
    android:text="@string/app_name" 
    /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentBottom="true" 
    android:background="@color/bluecolor" 
    > 
<EditText 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/edit_text_custom" 
    android:singleLine="true" 
    android:hint="@string/status" 
    android:textColor="#000" 
    android:textColorHint="#5222" 
    android:padding="7dp" 
    android:maxLength="50" 
    android:cursorVisible="true" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:drawableLeft="@android:drawable/ic_input_add" 
    android:ems="10" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="right|center_horizontal" 
    android:gravity="center" 
    android:layout_weight="0" 
    android:layout_marginBottom="10dp" 
    android:layout_marginRight="10dp" 
    android:padding="5dp" 
    android:text="@string/statusnumber" 
    android:background="@drawable/edit_text_custom" 
    /> 
</LinearLayout> 
</RelativeLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    > 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/whitecolor" 
    android:text="abcdfrg" 
    /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
    <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
    <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
     <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
     <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    />    
</LinearLayout> 
</LinearLayout> 
</ScrollView> 

回答

0

据我所知,如果你想在以同样的分屏两种尺寸,您必须在底部视图中采用另一个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:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" > 

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

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:orientation="vertical" > 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/stauslinearlayout" 
        android:layout_marginBottom="8dp" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text="@string/app_name" 
        android:textColor="@android:color/white" /> 

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

        <EditText 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="10dp" 
         android:layout_marginLeft="10dp" 
         android:layout_weight="1" 
         android:cursorVisible="true" 
         android:drawableLeft="@android:drawable/ic_input_add" 
         android:ems="10" 
         android:hint="status" 
         android:maxLength="50" 
         android:padding="7dp" 
         android:singleLine="true" 
         android:textColor="#000" 
         android:textColorHint="#5222" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_gravity="right|center_horizontal" 
         android:layout_marginBottom="10dp" 
         android:layout_marginRight="10dp" 
         android:layout_weight="0" 
         android:gravity="center" 
         android:padding="5dp" 
         android:text="statusnumber" /> 
       </LinearLayout> 
      </RelativeLayout> 

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

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

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/white" 
         android:text="abcdfrg" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 
       </LinearLayout> 
      </ScrollView> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

如果你不想采取另一个scrollView,那么你必须给wrap_contant底部线性布局。

<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:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" > 

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

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:orientation="vertical" > 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/stauslinearlayout" 
        android:layout_marginBottom="8dp" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text="@string/app_name" 
        android:textColor="@android:color/white" /> 

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

        <EditText 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="10dp" 
         android:layout_marginLeft="10dp" 
         android:layout_weight="1" 
         android:cursorVisible="true" 
         android:drawableLeft="@android:drawable/ic_input_add" 
         android:ems="10" 
         android:hint="status" 
         android:maxLength="50" 
         android:padding="7dp" 
         android:singleLine="true" 
         android:textColor="#000" 
         android:textColorHint="#5222" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_gravity="right|center_horizontal" 
         android:layout_marginBottom="10dp" 
         android:layout_marginRight="10dp" 
         android:layout_weight="0" 
         android:gravity="center" 
         android:padding="5dp" 
         android:text="statusnumber" /> 
       </LinearLayout> 
      </RelativeLayout> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/white" 
        android:text="abcdfrg" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

选择满足您的要求。

如果有问题呢,然后PLZ问... :-)

+0

我不能,因为我希望两个孩子布局与单滚动视图滚动使用第一种方法。第二种方法与我的问题具有相同的行为。不过,我设法通过动态设置第一个孩子身高到屏幕高度/ 2,然后动态地添加按钮到滚动视图。非常感谢! – Aviverma 2014-10-08 04:24:43

+0

@Aviverma,好的欢迎。 – Rumit 2014-10-08 04:28:48