Android TextView set picture and XML settings in code

Source: Internet
Author: User

To provide you with some methods, you can directly take to use, TextView in the code{code} set up and down around the settings picture. There are several ways to do this, and this gives you some examples.

The following methods are recommended in the Util tool class, where the benefits do not have to be told more.

,//resource ID public static void settextdrawable (context context, int drawableres,//resource ID
TextView tvname) {
drawable drawabletop = Context.getresources (). getdrawable (Drawableres);
The picture size must be set or not displayed
Drawabletop.setbounds (0, 0, drawabletop.getminimumwidth (),
Drawabletop.getminimumheight ());
Tvname.setcompounddrawables (NULL, drawabletop, NULL, NULL);
}

Bitmap

public static void Settextdrawable (context context, Bitmap Bitmap,
TextView tvname) {
drawable drawabletop = bitmaputils.bitmap2drawable (bitmap);
The picture size must be set or not displayed
Drawabletop.setbounds (0, 0, drawabletop.getminimumwidth (),
Drawabletop.getminimumheight ());
Tvname.setcompounddrawables (NULL, drawabletop, NULL, NULL);
}


Network URL Path

public static void Settextdrawable (context context, String URL,
Final TextView TextView) {
Imageloader.getinstance (). LoadImage (URL, new Imageloadinglistener () {
public void onloadingstarted (String arg0, View arg1) {
}

public void onloadingfailed (String arg0, View arg1, Failreason arg2) {
}

public void Onloadingcomplete (String arg0, View arg1, Bitmap Bitmap) {
drawable drawabletop = bitmaputils.bitmap2drawable (bitmap);
The picture size must be set or not displayed
Drawabletop.setbounds (0, 0, drawabletop.getminimumwidth (),
Drawabletop.getminimumheight ());
Textview.setcompounddrawables (NULL, drawabletop, NULL, NULL);
}

public void onloadingcancelled (String arg0, View arg1) {
}
});
}

There are three methods, each of which is set differently for each method. If there are other problems, I hope that the great God more guidance.


If you want to set up a picture in XML, then you just need a word android:drawabletop= "@drawable/ic_launcher"


This article is from the "Java" blog, so be sure to keep this source http://10594050.blog.51cto.com/10584050/1731578

Android TextView set picture and XML settings picture in code

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.