1

在我的 Android 应用程序的一个活动中,我有一个 ViewFlipper,里面有 3 个 ImageView,如下所示:

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

com.github.bumptech.glide:glide:3.7.0'我使用 Glide 库 ( ) 使用以下代码将图像从网络加载到 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();

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

你能帮我吗?

4

1 回答 1

1

我终于找到了解决方案。我将ViewFlippertomatch_parentwrap_content的宽度和高度以及内部RelativeLayouts 的宽度和高度更改为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>
于 2017-12-03T16:22:12.407 回答