如何在android中以编程方式在textview宽度内拟合图像宽度

时间:2018-03-21 04:30:22

标签: java android

我正在尝试在textview中添加位图图像,该文本视图在使用base64编码的DataBase中编码和存储。我使用以下代码

byte[]img= Base64.decode(cur.getString(2),Base64.DEFAULT);

 Drawable qDrawable = new BitmapDrawable(ctx.getResources(),BitmapFactory.decodeByteArray(img,0,img.length));

 question.setCompoundDrawablesWithIntrinsicBounds(null,null,qDrawable,null);

此代码工作正常,但

  

问题是图像宽度不适合文本视图宽度

我尝试在xml中使用ScaleType,但textview没有像imageview这样的属性,所以我如何用textview解决这个问题

1 个答案:

答案 0 :(得分:0)

您需要在设置之前调整位图大小。使用 <?xml version="1.0" encoding="utf-8"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> Drawable.setBounds()来获得所需的宽度和高度。

示例here