【问题标题】:Image background in imageView not changing by using setImageResource()imageView 中的图像背景不使用 setImageResource() 改变
【发布时间】:2015-09-16 10:24:09
【问题描述】:

我正在尝试为调用活动中单选按钮上的不同选项更改 ImageView 的背景图像。调用活动将捆绑中的参数传递给渲染活动,并且我正在正确接收参数(在单选按钮上做出选择后,我将 Curve1 和 Curve2 作为渲染活动中的参数)。但我无法通过 setImageResource() 或 setBackgroundResource() 更改图像背景。 这是 sn-p 这样做的:

渲染活动

private void validateImage()
    {       wave = (ImageView) findViewById(R.id.image_wave);
            switch (extras.getString("Curve"))
            {
                    case "Curve1":
                            wave.setB (R.drawable.sinewave1);
                            break;
                    case "Curve2":
                            wave.setImageResource(R.drawable.sinewave2);
                            break;
            }
     }

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"      
android:layout_width="match_parent"
android:layout_height="match_parent"     
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"           
tools:context="shantanu.concussion.usu.concussiontest.TestActivity">


<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image_wave"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

谁能建议我错过了什么? 谢谢:-)

【问题讨论】:

  • 参考stackoverflow.com/questions/11893826/… 对您的问题很有用
  • 我尝试在更改图像背景后使用 invalidate(),但没有帮助,我在 UI 线程上执行此操作,因此,invalidate 应该是重绘请求的选择。

标签: android android-imageview


【解决方案1】:

尝试手动调用无效。暴力改变最少的东西。您需要了解问题出在哪里(错误选择了View,View没有改变)例如尝试更改另一个属性。

可能导致资源中。 (例如,它是透明的)

检查分支开关的入口。 (可能是错误的比较)

通过编辑器更改属性。也许有什么东西阻止了显示背景。

这只是一个愚蠢的错误。

【讨论】:

  • 我做了一些类似的手动调试,但没有帮助。它肯定会进入开关盒,并且两个图像都是手动渲染的。但它没有在执行 setImageResources() 或 setBackgroundResources() 时设置图像。所以可能是我没有正确调用方法。
  • 方法调用被准确调用了吗?你试过直接打电话吗?
  • 你能解释一下吗?可能是我错过了你的意思。
  • 受保护的 void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); wave.setBackgroundResource(R.drawable.sinewave1); }
  • 试过了,没用。执行肯定会在 switch case 中进行,我尝试在 switch case 中记录一些信息,它工作得很好。
【解决方案2】:

试试

wave.setImageDrawable(getResources().getDrawable(R.drawable.sinewave2));

【讨论】:

  • getDrawable 在 API22 中已弃用,它不会刷新图像。我也尝试了一些与更改背景属性相关的其他方法,但它们没有帮助。仍在寻找解决方案。
  • 使用这个:ContextCompat.getDrawable(Context.Drawable ID)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多