1. Set the background of the View class
Android: background -- setbackgroundresource (INT) -- A drawable to use as the background.
Setbackgrounddrawable (drawable d) --- set the background to a given drawable, or remove the background.
Corresponding retrieval method:
Getbackground () --- gets the background drawable
2. Set the background of the imageview class
Android: SRC -- setimageresource (INT) --- sets a drawable as the content of this imageview.
Setimagedrawable (drawable) ---- sets a drawable as the content of this imageview.
Corresponding retrieval method:
Getdrawable () ---- return the view's drawable, or null if no drawable has been assigned.
Q: What is the difference between SRC and background in imageview?
When using SRC, the source image is displayed, and the image size should not be changed; when using background, the image is enlarged or reduced according to the size of the component.
3. Different images are displayed in the same imageview.
Sometimes, to display different images in the same imageview, we often use:
If (condition 1 ){
Image. setbackground (R. Id. XXX1 );
} Else if (condition 2 ){
Image. setbackground (R. Id. xxx2 );
}
You can use another simple method to implement the same function. First, create an XML file under Res/drawable with the following content:
<Level-list xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: maxlevel = "4" Android: drawable = "@ drawable/stat_sys_battery_0"/>
<Item Android: maxlevel = "14" Android: drawable = "@ drawable/stat_sys_battery_10"/>
<Item Android: maxlevel = "29" Android: drawable = "@ drawable/stat_sys_battery_20"/>
<Item Android: maxlevel = "49" Android: drawable = "@ drawable/stat_sys_battery_40"/>
<Item Android: maxlevel = "69" Android: drawable = "@ drawable/stat_sys_battery_60"/>
<Item Android: maxlevel = "89" Android: drawable = "@ drawable/stat_sys_battery_80"/>
<Item Android: maxlevel = "100" Android: drawable = "@ drawable/stat_sys_battery_100"/>
</Level-List>
Then, in layout, set the SRC of the Image view to a created XML file. When converting the image in the program, you only need to use:
Imageview. getdrawable (). setlevel (50 );
Android automatically selects the corresponding image based on the level value. This method is used to display different pictures of the remaining power displayed on the mobile phone.