gpt4 book ai didi

java - Android 以编程方式将图像设置为 TextView 的背景

转载 作者:太空狗 更新时间:2023-10-29 15:33:47 25 4
gpt4 key购买 nike

我有一个应用程序需要以编程方式将图像放在背景中,但它不起作用,代码是(来自 BankMenuParser 的 2 个方法):

    public void getProviderFields(String file, String providerId) throws DocumentException, IOException {

String xml = readFile(file);
Document doc = DocumentHelper.parseText(xml);

System.out.println("field name start");

List list = doc.selectNodes("//root/operators/operator[@id='" + providerId + "']/fields/field/name");
for (Iterator itr = list.iterator(); itr.hasNext();) {
// Attribute attr = (Attribute) itr.next();
// outList.add(attr.getValue());
Element el = (Element) itr.next();
Node elNode = (Node) itr.next();
// elNode.getText();
System.out.println("in array");
System.out.println("field name: " + elNode.getText().toString());
}
System.out.println("field name end");
}

public String getProviderImg(String file, String providerName) throws DocumentException, IOException {

String xml = readFile(file);
Document doc = DocumentHelper.parseText(xml);

String img = doc.selectSingleNode("//root/menu/group/operator_id[@name='" + providerName + "']/@image").getText();

return img;

}

public class Helpers {

public static int getResourceId(Context context, String name, String resourceType) {
return context.getResources().getIdentifier(name, resourceType, context.getPackageName());
}

These methods called in Activity:

Bundle menuExtras = getIntent().getExtras();

BankMenuParser bmp = new BankMenuParser();
String img = bmp.getProviderImg("data/menu.xml", menuExtras.getString("provider_name"));
TextView textView = (TextView) findViewById(R.id.provider_logo);
int resid = Helpers.getResourceId(PaymentActivity.this, img, "drawable");
textView.setBackgroundResource(resid);
bmp.getProviderFields("data/provider_fields.xml", menuExtras.getString("provider_id"));

我有兴趣在 LogCat 中将背景设置为图像,我看到的输出如下:

 07-13 13:27:36.178: I/System.out(677): mob_beeline.png

因此,应该在所有可绘制对象中正确设置图像我有一个 mob_beeline.png 和 layers.xml 内容:

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/mob_beeline"
android:gravity="center" />
</item>
</layer-list>

最佳答案

 textView.setBackgroundResource(R.drawable.mob_beeline);

See here

关于java - Android 以编程方式将图像设置为 TextView 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com