0

我的ImageView应用程序中有一个可以由用户更改的可绘制对象,并TextView在其上显示为黑色。如果用户将此图像更改为具有黑色背景的图像,他将无法看到文本。

如何识别图像资源是否R.drawable.imageA可以将文本设置为不同的颜色?

我想过为 设置一个标签ImageView,但是当我只关心两个选项时,我必须为每个选项设置它。

我试过了:

if (imageview.getDrawable.equals(R.drawable.imageA)) {
    textView.setText(Color.WHITE);
    }

也尝试过getResource,但都没有奏效。

4

3 回答 3

0
  1. Are you going to provide the user with a Dialog box / selection mechanism where the user gets to pick the image ?

  2. If the answer to the above is Yes, then this is the place where you should be able to capture this decision within your application, and then modify the color of the TextView .

  3. If the answer to 1 is no, then how does the user change the Image within your application ?

In your case, you may also be able to use reflection to achieve your goal. For example, take a look at this thread -> Android, getting resource ID from string?

Added later:

For a truly generalized solution, you may want to inspect the ImageView itself, to determine the optimal color to display. This has been demonstrated in this thread: Automatically change text color to assure readability

于 2012-11-09T00:26:38.737 回答
0

Aside from WebnetMobile.com's answer, you could just have a variable related to which image is currently displayed.

IE. When it starts up:

boolean isImageA = true;

And then change that to false when the event to change the background is triggered. All you have to do then is check whether it's true or false.

于 2012-11-09T00:27:07.770 回答
0

你不能。您需要自己将其存储在一边并用于比较,或者扩展 ImageView 并将该功能嵌入其中(即通过添加 getDrawableId())

于 2012-11-09T00:14:33.633 回答