我有一个TextView控件,它在一张图片上。当我点击按钮时,TextView的背景颜色会改变,但是图片不会消失。例如:
初始时我的TextView如下所示:
当我点击按钮后:
初始时我的TextView如下所示:
![enter image description here](https://istack.dev59.com/wlQ09.webp)
![enter image description here](https://istack.dev59.com/E90S8.webp)
如果你想仅使用一个 TextView
来完成这个任务,可能是不可能的。因此,我建议你使用 FrameLayout
完成。你可以按照以下方式编写布局。
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
TextView
背景颜色时,请按以下方式更改ImageView
背景...ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
或者当需要更多元素时,您还可以使用RelativeLayout并为您的元素提供相同的布局规则(例如,全部居中)。
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton是你的更好选择。图片源将会像你说的那样是星星,然后在点击时改变背景。如果想要在其中设置文本
android:drawableTop="@drawable/any_drawable"
android:text="@string/any_text"