4

我有一个相当简单的滚动视图,当我向上滚动时,它不会一直滚动到屏幕顶部。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.surreall.matchit"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:id="@+id/main_layout2"
    >

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

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="1dp"
            android:layout_gravity="center" 
            android:id="@+id/board"
            android:layout_weight="6"
            android:baselineAligned="false"

            >     
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1" 
            >

            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card1"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>

            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card2"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card3"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card4"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>

            </LinearLayout>  
       <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card5"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>

            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card6"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
           <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card7"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card8"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout> 
       <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:layout_gravity="center" 
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card9"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card10"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card11"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card12"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card13"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card14"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card15"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card16"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card17"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card18"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card19"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card20"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card21"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card22"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card23"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card24"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card25"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card26"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card27"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card28"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
            <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            >
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card29"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card30"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card31"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>
            <com.surreall.logo.SquareButton         
                android:layout_height="fill_parent"
                android:layout_width="0dip"          
                android:layout_weight="1"  >        
                <ImageView
                android:id="@+id/card32"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="0dp"          
                android:layout_weight="1" 
                android:layout_margin="4dp"
                /> </com.surreall.logo.SquareButton>       
            </LinearLayout>
     </LinearLayout> 
 </ScrollView>
</LinearLayout>

看起来像这样

4

3 回答 3

10

嗨,我遇到了同样的问题,经过调查,我找到了解决方案,在您的 XML 代码中,我可以看到您在布局中犯了同样的错误。

问题是您将 ScrollView 子 (LinearLayout) 布局重力设置为中心。删除此行,它应该适合您。

android:layout_gravity="center"   //remove this line
于 2014-02-03T13:16:53.377 回答
1

为什么将线性布局父级的高度设置为 0dp。它还应该 wrap_content 只进行更改并再次测试。

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

<LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  // changed to wrap content
        android:layout_marginTop="1dp"
        android:layout_gravity="center" 
        android:id="@+id/board"
        android:layout_weight="6"
        android:baselineAligned="false"

        >    

它应该可以正常工作...好吧,您也可以尝试对父布局进行此更改

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.surreall.matchit"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

android:orientation="vertical"
android:layout_width="match_content"         // changed to match_parent
android:layout_height="match_parent"         // changed to match parent
android:background="@drawable/background"
android:id="@+id/main_layout2"
>
于 2012-08-14T09:42:42.523 回答
0

您的布局中有几个问题:删除 topmost LinearLayoutScrollView应该只包含一个直接子级layout_height="match_parent",因此如果您想使用权重进行技巧,您需要在一个 LinearLayout 中执行它,该LinearLayout 将是 ScrollView 的单个子级。

于 2015-07-24T16:16:09.277 回答