简体   繁体   中英

ScrollView doesn't work with imageviews

I want to display 2 ImageView in my scrollview like this , but it doesn't work.

<ScrollView   
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/background_dark" 
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">


    <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">





<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />


<ImageView
    android:id="@+id/imageB"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/image"
    android:gravity="center" />

</LinearLayout>
</ScrollView> 

If i don't use android:fillViewport="true" , i see the 2 image but they are resized. If i use, size is correct but i can't get down to the second image. Why ?

Thanks

If you want to see both the images as per their sizes, you should use

<ImageView
...
android:layout_height="wrap_content"
.../>

instead of

<ImageView
...
android:layout_height="match_content"
.../>

Hope this helps.

You can get Screen width and height programmatically and then set the Width and height of ImageView respectively. Here is the code :

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try { 
display.getRealSize(size);
int width = size.x;
int height = size.y;
} catch (NoSuchMethodError e) {//to deal with API 8
int height = display.getHeight();
int width = display.getWidth();
}

Maybe the problem is that you have the image resources in the wrong folder? try removing the "fillViewport" and move the images on a lower dpi folder (from hdpi to mdpi or from mdpi to ldpi) this will change the "dp" (density-independent pixels) of the image, making them bigger on every screen.

Also, why both images have a width and height of "match_parent" ? wouldn't that make them both occupy the entire LinearLayout ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM