简体   繁体   English

有一个带有 3 个不同背景图像的按钮。 Android Studio、XML、Java

[英]Have a Button with 3 different background images. Android Studio, XML, Java

can somedy help me, and tell me whats wrong in this code?有人可以帮我,告诉我这段代码有什么问题吗? I have 3 different background pictures of my shack, (green, grey, normal) and in the Java Code below i check which state is active我有 3 张不同的小屋背景图片(绿色、灰色、正常),在下面的 Java 代码中,我检查哪个状态是活动的

编码

You can directly set the image of button in different conditions like this:-您可以在不同条件下直接设置按钮的图像,如下所示:-

((ImageButton)shackButton).setImageResource(R.drawable.<imageid>);

You can do it without creating any other resource file.您可以在不创建任何其他资源文件的情况下执行此操作。

change your xml to this将您的 xml 更改为此

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shack1000green" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/shack1000grey" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/shack1000" android:state_focused="false" android:state_pressed="false"/>
    </selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio (Java):直接使用 java 更改按钮的颜色 not.xml - Android Studio (Java): Changing Color of a Button directly with java not .xml 如何在xml / java中使按钮颜色可变(android studio) - how to make button color variable in xml/java (android studio) 内存不足处理图像时出现异常。 在java中 - Out of memory Exception while dealing with images. in java Android Studio:1个按钮中2个不同的活动 - Android Studio: 2 different activities in 1 button Android Studio:Java或XML问题? - Android Studio: Java or XML issue? 当按钮具有特定的当前文本时,如何更改按钮上的文本(Android Studio / Java和XML) - How to change text on button when the button has a particular current text (Android Studio/Java&XML) 为什么我的Android模拟器的背景与main.xml不同? - Why does my android emulator have different background than main.xml ? Java.xml 创建可绘制按钮背景 - Java .xml creating a drawable button background android 工作室中的按钮背景颜色始终为紫色 - button background color is always purple in android studio 如何使用 java 代码而不是 xml 代码将按钮添加到 android 工作室中的另一个活动 - How do I add button to another activity in android studio using java code rather than xml code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM