如何为线性布局设置两个不同的背景
我有一个线性布局,并且在其上实现了一个 onClickListener 。 现在我希望当单击它时,其背景颜色更改为白色,并保持这种状态,直到单击其他内容。当单击其他内容时,我希望它具有透明背景 如何实现这一目标? 提前致谢
I have a linear layout and there is an onClickListener implemented on it.
Now I want that when it is clicked its background color to white is changed and remains that way until something else is clicked. When something else is clicked I want it to have transparent background
How to achieve this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在可绘制文件夹中创建两个可绘制图像。单击它时,您可以更改布局的背景。
以下代码更改背景:
Create two drawable images in your drawable folder. And when it is clicked, you can change the background of the layout.
Following code changes the background:
我认为你也可以使用选择器作为背景。您可以利用“选定”或“聚焦”状态在透明和白色之间切换背景。它看起来像这样:
然后在您的可绘制文件夹中添加
bg_list_selector.xml
:我想说,尝试一下 StateListDrawable 的不同选项。
I think you could also just use a selector as background. You can probably take advantage of the 'selected' or 'focused' states to toggle the background between transparent and white. It'll look something like:
And then
bg_list_selector.xml
in your drawable folder:Have a play with the StateListDrawable's different options, I'd say.