Android 如何使左侧带有drawable的textview可以从服务器的url以编程方式设置

Android 如何使左侧带有drawable的textview可以从服务器的url以编程方式设置,android,android-layout,textview,Android,Android Layout,Textview,如何使左侧的TextView具有可绘制的Drawable,可通过服务器的url进行编程设置。使用Volley类加载图像 好的,第一步是从URL解码图像 你可以这样做: String yourUrl = "http://someUrl"; // insert your URL here // connect, get an instance of the InputStream HttpURLConnection connection = (HttpURLConnection) new URL(


如何使左侧的
TextView
具有可绘制的
Drawable
,可通过服务器的url进行编程设置。

使用Volley类加载图像
好的,第一步是从URL解码图像

你可以这样做:

String yourUrl = "http://someUrl"; // insert your URL here

// connect, get an instance of the InputStream
HttpURLConnection connection = (HttpURLConnection) new URL(yourUrl).openConnection();
InputStream inputStream = connection.getInputStream();

// decode the stream into a Bitmap and create a Drawable from it
Bitmap tempBitmap = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(getResources(), tempBitmap);
然后,将其设置为可在
文本视图的左侧绘制的复合图形:

// the order is left, top, right, bottom, so you need to set the first param
yourTextView.setCompoundDrawables(drawable, null, null, null);

您发出网络请求并更新ImageView。请分享您为实现此功能而尝试研究的内容。问题是什么?可能重复: