2010-09-09 88 views
8

alt text
如何增加Android中的ScrollView高度?

喜的朋友,

我使用GridView一个ScrollView内用于显示图像。在GridView中,我有16个动态添加的图像,但ScrollView不显示全部16个图像(请参见屏幕截图)。

我想让ScrollView显示整个GridView,有谁知道我该如何解决这个问题?

谢谢大家。

xml文件:在Java代码XML布局或使用方法setFillViewport(true)

<?xml version="1.0" encoding="utf-8"?> 
<merge android:layout_width="wrap_content" android:layout_height="340dip" xmlns:android="http://schemas.android.com/apk/res/android"> 
<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="320dip" android:orientation="vertical" 
android:background="@color/black"> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/scrollview" 
      android:layout_width="fill_parent" 
      android:layout_height="500dip" 
      android:background="#ffffff" 
      android:paddingTop="10dip" 
      android:paddingLeft="5dip" 
      android:layout_weight="1"> 

<GridView 
        android:id="@+id/jr_lookbook_grid" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:numColumns="4" 
        android:verticalSpacing="10dp" android:horizontalSpacing="10dp" 
        android:columnWidth="90dp" android:stretchMode="columnWidth" 
        android:adjustViewBounds="true" 
        android:background="@drawable/shape"  

        android:gravity="center" android:layout_weight="1"/> 
</ScrollView> 

        <Button android:id="@+id/click" 
        android:background="@android:color/transparent" 
        android:text="Load More Pictures..." 

        android:textColor="@color/white" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_centerVertical="true" /> 

</LinearLayout> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout02_img" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_gravity="center" 
    android:layout_alignParentBottom="true" 
    android:background="@color/black" 
       android:layout_alignParentLeft="true"> 

    <WebView 
     android:id="@+id/webview" 

     android:layout_width="fill_parent" 

     android:layout_height="fill_parent"   
     android:scrollbars="none" 

    /> 
    <ImageView android:id="@+id/ImageView01" 
    android:layout_gravity="center_horizontal" 
    android:scaleType="centerInside" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 

    android:adjustViewBounds="true"> 
</ImageView> 

    </LinearLayout> 
    <LinearLayout android:id="@+id/LinearLayout02"  android:background="#AA000000" 
     android:layout_width="400px" 
     android:layout_height="50dip" 
     android:layout_gravity="bottom"  
      > 
          <Button 
       android:id="@+id/back" 

       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/back1" 
       android:layout_alignParentRight="true" 
       android:layout_marginLeft="10dip" 

       /> 

       <Button 
        android:background="@drawable/forward5" 
       android:id="@+id/next" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:layout_alignParentRight="true" 
       android:layout_marginLeft="215dip" 

       /> 
</LinearLayout> 

</merge> 

回答

32

使用属性android:fillViewport="true"

看一看:ScrollView documentation

+0

谢谢!这个问题花了我一些讨厌的时间:) – 2012-04-03 07:59:38

+1

都没有工作... – bubakazouba 2016-02-25 03:59:40

0

如果布局重量的layout &顶部部分的底部部分是0,则设置滚动视图1和使用layout_height="match_parent"

0

据我所知,没有必要为网格视图添加滚动视图。在Android中,如果有任何需要,它会自动添加滚动视图。

3

您不应该将GridView添加到ScrollView中。请参考GridView API了解GridView。

根据定义,GridView显示二维可滚动网格中的项目。因此,如果你想在ScrollView中添加一个可滚动组件,行为将不会如预期的那样。由于我们无法在ScrollView中添加ListView,因此我们不能也不应该在ScrollView中添加GridView。

4

我有一个类似的问题,我有一个AbsoluteLayout上的固定导航栏下的滚动视图。

我将scrollview高度设置为屏幕高度减去导航栏的高度。 我的屏幕高度为480dp,导航栏高度为47dp,因此我在xml布局中将scrollview高度设置为433dp,并使用Y偏移将scrollview的顶部向下移动到导航栏下方。

<ScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="fill_parent" 
    android:layout_height="433dp" 
    android:layout_x="0dp" 
    android:layout_y="50dp" 
    android:fillViewport="true" > 

添加以下的进口:

import android.content.Context; 
import android.view.WindowManager; 
import android.widget.ScrollView; 
import android.view.Display; 

现在,使之成为多屏工作,我已将此添加的onCreate的活动:

ScrollView myScrollView = (ScrollView) findViewById(R.id.scroll_view); 
    myScrollView.getLayoutParams().height = ((int) ScHgt(this)-47); 

有了这个辅助函数的帮助得到屏幕高度:

//---------------------------------------------------------------------------------------------- 
// public static double ScHgt(Context context) 
//---------------------------------------------------------------------------------------------- 
    //return the screen height of the device 
public static double ScHgt(Context context) 
{ 
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = windowManager.getDefaultDisplay(); 
    return display.getHeight(); 
}