TL; DR:AndroidのメーカーではないのはなぜAndroidのJavaの - (構文解析にはDocumentBuilderを使用した)のres /レイアウトxmlファイル
File rewardFile = new File("res/layout/reward_cards.xml");
仕事ですか?
Android Studioでres/layoutファイルを参照して、解析するDocumentBuilder Documentインスタンスを取得する際に問題があります。
私がしようとしているのは、可変量のCardViewオブジェクトをRelativeLayoutオブジェクトの下部に生成することです。
現在の設定では、別のレイアウトファイルからCardViewオブジェクトを膨張させ、既存のRelativeLayoutビューに追加します。その後、追加する次のCardViewを表すようにCardView XMLを変更し、レイアウトファイルから再膨張させます。
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レイアウトファイルを参照するにはどうすればよいですか?
私がやろうとしていることを達成するための方がはるかに簡単ですか?
この方法でも機能しますか?
おかげ