简体   繁体   English

如何从以字符串形式提供的XML源代码创建Drawable(或XmlResourceParser对象)?

[英]How can I create Drawable (or XmlResourceParser object) from XML source code provided as a string?

How can I create Drawable from XML source code provided as a string ? 如何从以字符串形式提供的 XML源代码创建Drawable

I found the following method: 我发现以下方法:

Drawable drawable = Drawable.createFromXml();

But it requires to provide XmlResourceParser - and I still don't see a way to create it from XML source code. 但是它需要提供XmlResourceParser我仍然看不到从XML源代码创建它的方法。

Sample of the XML source code: XML源代码示例:

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

FYI FYI

createFromXml()

Create a drawable from an XML document using an optional Resources.Theme. 使用可选的Resources.Theme从XML文档创建一个可绘制对象。

Try this 尝试这个

public class RecyclerViewActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        Drawable testDrawable;

        Resources res = getResources();
        try {
            testDrawable = Drawable.createFromXml(res, res.getXml(R.xml.test));
        } catch (Exception ex) {
            Log.e("Error", "Exception creating drawable from XML");
        }

    }


}

I just think of this, maybe not exactly syntactically correct but I believe this is something you want. 我只是想到了这一点,也许在语法上并不完全正确,但是我相信这是您想要的。

public Drawable xmlStringToDrawable(String yourString){
     XmlPullParser parser = Xml.newPullParser();
     parser.setInput(new StringReader(yourString));
     return Drawable.createFromXml(getResources(),parser)
}

This is not an answer to the question. 这不是问题的答案。 But it solved my problem. 但这解决了我的问题。 Thanks everyone for the help: 感谢大家的帮助:

        Drawable icon = resources.getDrawable(resources.obtainTypedArray(R.array.arrayName).getResourceId(index, 0), context.getTheme());
        BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
        bitmapDrawable.setGravity(Gravity.RIGHT);

I generate only index on run-time in this approach. 我使用这种方法仅在运行时生成index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中从String创建XML对象? - How to create a XML object from String in Java? 如何使用 Java Stream 修复此嵌套集合代码,以便从 object 的属性创建字符串数组? - How can I fix this nested collection code using Java Stream in order to create an array of string from the property of an object? 如何从Drawable接收链接? - How can I receive link from Drawable? 从排序字符串对象数组(提供的代码)中得到奇怪的输出? - Getting weird output from sorting string object arrays (code provided)? 如何以编程方式将 XML drawable 设置为背景图像? - How can I set an XML drawable as a background image programatically? 如何将可绘制对象的 int ID 存储在 XML TypedArray 中 - How can I store the int ID of a drawable in an XML TypedArray 如何解析“ddMM”格式的日期(以字符串形式提供)并将其转换为 Java 中的日期对象? - How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java? 如何运行从Android源代码获得的应用程序源代码? - How can I run an app's source code that I got from the Android source code? 使用 JAXB 从 XML 字符串创建对象 - Use JAXB to create Object from XML String 我可以在XML中存储drawable的int ID吗? - Can I store the int IDs of drawable in an XML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM