【问题标题】:How to increase ScrollView height in Android?如何在 Android 中增加 ScrollView 的高度?
【发布时间】:2010-09-09 09:07:32
【问题描述】:


朋友们,

我在ScrollView 中使用GridView 来显示图像。在 GridView 我有 16 个动态添加的图像,但 ScrollView 没有显示所有 16 个图像(见屏幕截图)。

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

谢谢大家。

xml 文件:

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

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

    在 XML 布局中使用属性 android:fillViewport="true" 或在 Java 代码中使用方法 setFillViewport(true)

    看看:ScrollView documentation

    【讨论】:

    • 谢谢!这个问题花了我几个小时 :)
    【解决方案2】:

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

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

    <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();
    } 
    

    【讨论】:

      【解决方案3】:

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

      根据定义 GridView 在二维可滚动网格中显示项目。因此,如果您要在 ScrollView 中添加可滚动组件,则行为将与预期不同。由于我们不能在 ScrollView 中添加 ListView,我们不能也不应该在 ScrollView 中添加 GridView。

      【讨论】:

        【解决方案4】:

        据我所知,不需要为 Grid View 添加滚动视图。在Android中,如果需要,它会自动添加滚动视图。

        【讨论】:

          【解决方案5】:

          如果layout的底部和布局权重的顶部为0,则将滚动视图设置为1并使用layout_height="match_parent"

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-21
            • 2012-10-13
            • 1970-01-01
            • 2013-11-28
            • 2016-04-15
            • 1970-01-01
            相关资源
            最近更新 更多