【问题标题】:ScrollView is not scrolling overflowed layoutScrollView 没有滚动溢出的布局
【发布时间】:2014-10-06 18:12:41
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-layout layout


    【解决方案1】:

    AFAIK,如果要将屏幕分成相同的两种尺寸,则必须在底部视图中使用另一个滚动视图,如下例所示...

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

    如果你不想使用另一个滚动视图,那么你必须将 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>
    

    选择符合您的要求。

    如果有任何问题,请询问... :-)

    【讨论】:

    • 我不能使用第一种方法,因为我希望两个子布局都使用单个滚动视图滚动。而第二种方法是给出与我的问题相同的行为。但是,我设法通过将第一个子高度动态设置为屏幕高度/2,然后动态地将按钮添加到滚动视图来做到这一点。非常感谢!
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多