【问题标题】:Android Java - Using DocumentBuilder to parse() a res/layout xml fileAndroid Java - 使用 DocumentBuilder 解析() res/layout xml 文件
【发布时间】:2016-10-06 15:54:31
【问题描述】:

TL;DR:为什么不

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

在 Android Studio 中工作?

我无法在 Android Studio 中引用 res/layout 文件以获取 DocumentBuilder 文档实例来解析它。

我要做的是在 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 布局文件?

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

这种方法还能用吗?

谢谢

【问题讨论】:

    标签: java android xml file android-layout


    【解决方案1】:

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

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

    然后,修改 CardView XML 以表示下一个要添加的 CardView,并从布局文件中重新膨胀。

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

    这种方法还能用吗?

    没有。

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

    第 1 步:扩展布局。

    第 2 步:调用 CardView 及其内容上的方法以根据需要进行更改。

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

    第 4 步:起泡、冲洗、重复。

    如果您要拥有许多这样的CardView 容器,请考虑使用RecyclerViewListView 而不是RelativeLayout

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 2017-10-15
      • 2010-09-25
      • 1970-01-01
      • 2012-04-24
      • 2011-09-30
      • 2013-06-25
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多