java - 如何使用 imageView 一次显示一张图像

标签 java android xml

我的问题是我想在屏幕上一次显示一张图像,并且每天我的图像都会发生变化。我正在使用 imageView,但它显示 notification.xml 的所有图像。我搜索了很多,我认为我错过了一些简单的事情。请帮忙。

Notification.java 是:

    package com.example.customcalendarandroid;
    import java.io.File;



    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.app.Activity;
    import android.graphics.drawable.Drawable;

    public class NotificationView extends Activity{
       @Override
       public void onCreate(Bundle savedInstanceState)
       {
          super.onCreate(savedInstanceState);
         setContentView(R.layout.notification);
          //setContentView(R.layout.newxml);

         int i=10;
         if(i==10){
             ImageView   imgView = (ImageView) findViewById(R.id.ekadashi_name2);
             imgView.setImageResource(R.drawable.prabhu2);
         }else
         {
             ImageView   imgView = (ImageView) findViewById(R.id.ekadashi_name3);
             imgView.setImageResource(R.drawable.prabhu3); 
         }
         }
and 

notification.xml is

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    >

    <ImageView
         android:id="@+id/ekadashi_name"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/krishna" />

  <ImageView
         android:id="@+id/ekadashi_name1"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhupada" />
     <ImageView
         android:id="@+id/ekadashi_name2"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu2" />
     <ImageView
         android:id="@+id/ekadashi_name3"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu3" />
     <ImageView
         android:id="@+id/ekadashi_name4"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu4" />
     <ImageView
         android:id="@+id/ekadashi_name5"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu5" />
     <ImageView
         android:id="@+id/ekadashi_name6"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu6" />




    <ImageView
         android:id="@+id/ekadashi_name"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhupada1" />


</LinearLayout>

最佳答案

您可以使用图像数组 喜欢

    private int[] imagesArray = {
    R.drawable.img_00,
    R.drawable.img_01,
    R.drawable.img_02,
    };

那么你可以这样做

    Drawable d = getResources().getDrawable(imagesArray[i]);
    yourImageView.setImageDrawable(d);

每次要加载图像时只需更改 int i 值即可

在 xml 中只保留一个可以工作的 ImageView ..u 为不同的图像保留不同的 ImageView

希望对你有帮助

关于java - 如何使用 imageView 一次显示一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059099/

相关文章:

java - Android:修改Listview上的项目

xml - 改进功能以使用标签从字符串填充对象

java - 自定义 JComboBox 隐藏 JPopupMenu

java - 使用 @Capture Mockito 进行测试时出现问题

android - 如何在 Android Webkit 或 Opera Mobile 10 中获取 GPS 位置

xml - 数据结构格式(YAML 或诸如此类)的往返解析,保留注释,用于编写配置

java - 如何从 Java 中的 map 列表中获取所有值?

java - "postpone"任务真的那么难(或 "wait for idle")

Android传感器和线程

android - Java Restful Web 服务仅运行 Android 4.4 Mobile 而不是其他