【问题标题】:ViewFlipper images aren't being viewed未查看 ViewFlipper 图像
【发布时间】:2017-12-02 18:07:39
【问题描述】:

在我的一个 Android 应用程序的 Activity 中,我有一个 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>

我使用 Glide 库 (com.github.bumptech.glide:glide:3.7.0') 使用以下代码将图像从网络加载到 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 时,我在屏幕上看到了图像。基本上没有什么应该包含错误,但是由于某种原因,当我运行应用程序时,我在屏幕上看不到任何图像。

你能帮帮我吗?

【问题讨论】:

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


    【解决方案1】:

    我终于找到了解决办法。我将ViewFlipper的宽度和高度更改为match_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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-03
      • 2016-02-02
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多