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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11472294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:15:43  来源:igfitidea点击:

Android set images to background of a TextView programmatically

javaandroidimagetextview

提问by Arthur Kushman

I have an app where need to put images to background programmatically, but it doesn't work, the code is (2 methods from BankMenuParser):

我有一个应用程序需要以编程方式将图像置于后台,但它不起作用,代码是(来自 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"));

I'm interested in set the background to the image in LogCat I see the output like:

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

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

So, the image should be set right in all drawables I have a mob_beeline.png and layers.xml with content:

因此,图像应该在所有可绘制对象中设置正确,我有一个带有内容的 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> 

回答by Samir Mangroliya

 textView.setBackgroundResource(R.drawable.mob_beeline);

See here

看这里