Как увеличить высоту ScrollView в Android? - PullRequest
9 голосов
/ 09 сентября 2010

alt text

Привет друзья,

Я использую GridView внутри ScrollView для отображения изображений. В 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>

Ответы [ 5 ]

33 голосов
/ 08 февраля 2011

Использовать атрибут android:fillViewport="true" в макете XML или использовать метод setFillViewport(true) в коде Java.

Посмотрите: Документация ScrollView

4 голосов
/ 17 апреля 2013

У меня была похожая проблема, когда у меня было прокручиваемое изображение под фиксированной панелью навигации на AbsoluteLayout.

Я установил высоту прокрутки на высоту моего экрана за вычетом высоты панели навигации. Мой экран был 480dp в высоту, а navbar был в 47dp, поэтому я установил высоту scrollview на 433dp в макете xml и переместил верхнюю часть scrollview вниз под navbar, используя смещение 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 голосов
/ 11 апреля 2013

Не следует добавлять GridView в ScrollView. Пожалуйста, обратитесь к GridView API для понимания GridView.

Согласно определению GridView отображает элементы в двумерной сетке с возможностью прокрутки. Следовательно, если вы добавите прокручиваемый компонент в ScrollView, поведение будет не таким, как ожидалось. Поскольку мы не можем добавить ListView внутри ScrollView, мы не можем и не должны добавлять GridView внутри ScrollView.

0 голосов
/ 26 ноября 2012

По моему мнению, нет необходимости добавлять вид прокрутки для вида сетки.В Android автоматически добавляется представление прокрутки, если в этом есть необходимость.

0 голосов
/ 18 мая 2012

Если нижняя часть layout и верхняя часть веса макета равны 0, тогда установите режим прокрутки на 1 и используйте layout_height="match_parent".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...