开发者

How to increase ScrollView height in Android?

开发者 https://www.devze.com 2023-01-15 16:42 出处:网络
Hi friends, I am using GridView inside aScrollView for displaying images. In the GridView I have 16 images which were dynamically added, but the ScrollView does not display all 16 images (see scre

How to increase ScrollView height in Android?

Hi friends,

I am using GridView inside a ScrollView for displaying images. In the GridView I have 16 images which were dynamically added, but the ScrollView does not display all 16 images (see screenshot).

I want the ScrollView to display the whole GridView, does anybody know how I can fix this?

Thanks all.

xml file:

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

              />

                 <Butt开发者_C百科on
                  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>


Use attribute android:fillViewport="true" in XML layout or use method setFillViewport(true) in Java code.

Have a look at : ScrollView documentation


I had a similar issue where I had a scrollview under a fixed Navigation bar on an AbsoluteLayout.

I set the scrollview height to the height of my screen less the height of the navbar. My screen was 480dp high and the navbar was 47dp high so I set the scrollview height to 433dp in the xml layout and moved the top of the scrollview down below the navbar using a Y offset.

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

Add the following imports:

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

Now to make it work for multiple screens I added this to onCreate for the activity:

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

With the help of this helper function to get the screen height:

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


You should not add GridView into ScrollView. Please refer the GridView API for understanding GridView.

As per the definition GridView displays the items in two dimensional scrollable grid. Hence, if you would add a scrollable component inside ScrollView the behavior would not be as expected. As we cannot add ListView inside ScrollView we cannot and should not add GridView inside ScrollView.


According to me, there is no need to add scroll view for Grid View. In Android, it will add scroll view automatically, if there is any need.


If the bottom part of the layout & top part of the layout weight are 0, then set the scroll views to 1 and use layout_height="match_parent".

0

精彩评论

暂无评论...
验证码 换一张
取 消