2012-08-14 103 views
3

我有一个相当简单的滚动视图,当我向上滚动时,不滚动到屏幕顶部。滚动查看不滚动到线性布局顶部

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

Looks like this

+0

告诉我的布局xml文件完全 – jenuine 2012-08-14 09:18:14

+0

我添加的代码 – clayton33 2012-08-14 09:21:52

+1

哪里是在布局滚动视图的其余部分 – jenuine 2012-08-14 09:24:30

回答

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" 
> 
+0

我是新来的这scrollview的东西,我认为从另一个项目复制该代码。我改变了你提到的那一行,但行为是一样的:? – clayton33 2012-08-14 09:51:35

0

试试这个代码:

scrollView.scrollTo(0, 0); 
4

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

问题是,您将ScrollView子(LinearLayout)布局重力设置为居中。删除这一行,它应该为你工作。

android:layout_gravity="center" //remove this line 
0

有几个在你的布局问题:去掉最上面的LinearLayoutScrollView应该只包含一个直接孩子layout_height="match_parent",所以如果你想使配重块,你需要做一个的LinearLayout是将里面的窍门是ScrollView的单身孩子。