How is the image of Android ImageButton defined?

Source: Internet
Author: User

============ Problem Description ============


I have customized a view object that adds an ImageButton object to the object's inner line item, and the object's picture is set using SRC, The image of the ImageButton object is set by the Setbackgroundresource method within the OnClick event of the button, but two of the corresponding images are not the same, and the former is set up with a square button to display below. The latter setting will not show the square button, how to set the Setbackgroudresource picture inside the XML, do not display the button box;

============ Solution 1============


You need to use drawable to achieve AH. Why write code?

In addition, in fact, ImageButton relatively unpopular, are used ImageView

============ Solution 2============


ANDROID:SRC set is the picture display content, of course, and Setbackgroundresource settings background is not the same, you should set imageview.setimagedrawable, this to set the display content, so that and Android: The SRC settings are consistent.

============ Solution 3============


Reference 6 floor HACKERLYF reply:
View.setbackgroundresource can set the background of the picture, with what command to get the background image of the view?

View.getbackground ()

============ Solution 4============


You can save the state with preference, or you can set a global identity and change the status once.

============ Solution 5============


I think that you should use Android selector, since you are some kind of button to open and close the picture state, directly under the Drawable folder to build an XML folder,

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/ic_launcher "android:state_enabled=" false "/> <item android: drawable= "@drawable/ic_launcher"/></selector>


You use this file as an ordinary drawable resource

When you need to change the status of the picture, set the view to enable, Android will help you to switch the corresponding image

============ Solution 6============


It is best to use ImageView, just as there are click events, why use ImageButton

============ Solution 7============


Do you mean that you need to set the view to show only pictures without showing the background box? , you can set the XML file's
Android:background= "#00000000"
android:src= "@drawable/xxx"
The first sentence sets the background to full transparency to see if it is feasible.

============ Solution 8============


Reference 8 Floor Hackerlyf's reply:
Quote: Referring to the reply of the 7 floor Lionfresh:

Quote: Referring to the reply of the 6 floor HACKERLYF:

View.setbackgroundresource can set the background of the picture, with what command to get the background image of the view?

View.getbackground ()

I have two pictures, one is open, one is off, the initialization is off, I want to determine whether the background of the view is open picture or close picture, these two pictures are placed in the drawable directory, through View.getbackgroud () is a drawable object, R.drawable.lighton is an int object, how should we compare and judge it?

This is not possible unless you save the Drawable object in SetBackground ().
However, depending on your needs, you should not judge the business logic by the changes in the picture, but should be judged by the data you display.

How is the image of Android ImageButton defined?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.