ViewFlipper图像未被查看

时间:2017-12-02 18:07:39

标签: android imageview android-glide viewflipper android-viewflipper

在我的一个Android应用程序的活动中,我有一个ViewFlipper,里面有3个ImageViews,构建如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.app.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:layout_margin="8dp"
    android:background="#fff">

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

        <ViewFlipper
            android:id="@+id/flipper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:scaleType="center"
                android:id="@+id/imageFlipper1"
                android:adjustViewBounds="true" />

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:scaleType="center"
                android:id="@+id/imageFlipper2"
                android:adjustViewBounds="true" />

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:scaleType="center"
                android:id="@+id/imageFlipper3"
                android:adjustViewBounds="true" />

        </ViewFlipper>

    </LinearLayout>

</ScrollView>

我使用以下代码使用Glide库(com.github.bumptech.glide:glide:3.7.0')将图像从Web加载到ImageViews:

flipper = (ViewFlipper) findViewById(R.id.flipper);
imageFlipper1 = (ImageView) findViewById(R.id.imageFlipper1);
imageFlipper2 = (ImageView) findViewById(R.id.imageFlipper2);
imageFlipper3 = (ImageView) findViewById(R.id.imageFlipper3);

// Load images
Glide.with(getApplicationContext()).load("url1").into(imageFlipper1);
Glide.with(getApplicationContext()).load("url2").into(imageFlipper2);
Glide.with(getApplicationContext()).load("url3").into(imageFlipper3);

// Setup the ViewFlipper
flipper.setAutoStart(true);
flipper.setFlipInterval(2000);
flipper.startFlipping();

网址有效,所以当我尝试将图片加载到独立的ImageView时,我在屏幕上看到了图像。基本上没有什么应该包含错误,但出于某种原因,当我运行应用程序时,我在屏幕上看不到任何图像。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我终于找到了解决方案。我将ViewFlipper的宽度和高度更改为match_parentwrap_content,将内部RelativeLayout的宽度和高度更改为match_parent,现在我可以看到我活动上的图片:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.app.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:layout_margin="8dp"
    android:background="#fff">

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

        <ViewFlipper
            android:id="@+id/flipper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center">

                <ImageView
                    android:id="@+id/flipper1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:foregroundGravity="center"
                    android:scaleType="fitCenter"/>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center">

                <ImageView
                    android:id="@+id/flipper2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:foregroundGravity="center"
                    android:scaleType="fitCenter"/>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center">

                <ImageView
                    android:id="@+id/flipper3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:foregroundGravity="center"
                    android:scaleType="fitCenter"/>

            </RelativeLayout>

        </ViewFlipper>

    </LinearLayout>

</ScrollView>
相关问题