繁体   English   中英

Android Java-使用DocumentBuilder解析()一个res / layout xml文件

[英]Android Java - Using DocumentBuilder to parse() a res/layout xml file

TL; DR:为什么不

    File rewardFile = new File("res/layout/reward_cards.xml");

在Android Studio中工作?

我无法在Android Studio中引用res / layout文件来获取DocumentBuilder Document实例进行解析。

我想做的是在RelativeLayout对象的底部生成可变数量的CardView对象。

我当前的设置涉及从单独的布局文件扩展CardView对象,并将其添加到现有的RelativeLayout视图中。 然后,修改CardView XML以表示要添加的下一个CardView,然后从布局文件重新进行充气。

private void generateRewards(Context context, RelativeLayout mainRL) {
    LayoutInflater inflater = LayoutInflater.from(context);

    View rewardLayout = inflater.inflate(R.layout.reward_cards, null, false);

    RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rParams.addRule(RelativeLayout.BELOW, mainRL.findViewById(R.id.cardFutureGoals).getId()); //This is the current bottom view

    mainRL.addView(rewardLayout, rParams);

    //Now to edit the XML

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder docBuilder = null;
    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    File rewardFile = new File("res/layout/reward_cards.xml");

    Document rewardDoc = null;
    if(rewardFile.exists()) {  //This Fails
        try {
            rewardDoc = docBuilder.parse(rewardFile);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Just changing the CardView's id for now

    Node rootCard = null;
    rootCard = rewardDoc.getFirstChild();
    NamedNodeMap attr = rootCard.getAttributes();
    Node rewardId = attr.getNamedItem("id");
    rewardId.setTextContent("@+id/reward2");

    Transformer t = null;
    try {
        t = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }

    DOMSource domSource = new DOMSource(rewardDoc);
    StreamResult s = new StreamResult("res/layout/reward_cards.xml"); //This will probably also fail
    try {
        t.transform(domSource, s);
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    ...

    //Inflate again and add the next cardview

    rewardLayout = inflater.inflate(R.layout.reward_cards, null, false);

    rParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rParams.addRule(RelativeLayout.BELOW, mainRL.findViewById(R.id.reward1).getId());

    mainRL.addView(rewardLayout, rParams);
}

这是我的reward_cards.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reward1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.30"
    android:background="#00CF98"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:background="#ffffff">

        ...

    </RelativeLayout>


</android.support.v7.widget.CardView>

因此,添加下一个CardView最终将被放入循环,但是我只是在尝试获取

File rewardFile = new File("res/layout/reward_cards.xml");

在这一点上工作。

如何在此处引用reward_cards布局文件?

有没有更简单的方法可以完成我想做的事情?

这种方法还能用吗?

谢谢

TL; DR:为什么不File rewardFile = new File("res/layout/reward_cards.xml"); 在Android Studio中工作?

因为资源仅是开发机器上的文件。 它不是设备上的文件。

然后,修改CardView XML以表示要添加的下一个CardView,然后从布局文件重新进行充气。

您不能在运行时修改资源。

这种方法还能用吗?

没有。

有没有更简单的方法可以完成我想做的事情?

步骤#1:为版面充气。

步骤#2:在CardView及其内容上调用方法,以根据CardView对其进行更改。

步骤#3:将修改后的CardView添加到RelativeLayout

步骤#4:起泡,冲洗,重复。

如果将有许多这样的CardView容器,请考虑使用RecyclerViewListView代替RelativeLayout

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM