是否可以在onCreate()中生成main.xml?如:
package avm.project;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class AVMOrderSystemActivity extends Activity {
ViewFlipper flipper;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//File[] listOfFiles = folder.listFiles();
/*for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
Log.i("XML Generator","File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
Log.i("XML Generator","Directory " + listOfFiles[i].getName());
}
}*/
String xml="";
xml=xml+"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
xml=xml+"<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"+
"android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"\n"+
"android:orientation=\"horizontal\" android:padding= \"5px\">\n";
xml=xml+"<ViewFlipper android:id=\"@+id/adflipper\"\n"+
"android:inAnimation=\"@android:anim/fade_in\" android:outAnimation=\"@android:anim/slide_out_right\"\n"+
"android:paddingLeft=\"15px\" android:layout_width=\"fill_parent\"\n"+
"android:layout_height=\"fill_parent\" android:autoStart=\"true\"\n"+
"android:flipInterval=\"5000\">";
Resources a=this.getResources();
try
{
for (int i=0x7f020000 ;i<0x7f020020;i++)
{
String name=a.getResourceName(i);
xml=xml+"<ImageView android:layout_width=\"fill_parent\" android:src=\"@"+name.substring(name.indexOf(':')+1)+"\"\n"+
"android:layout_height=\"fill_parent\"></ImageView>\n";
}
}
catch(Exception e)
{
Log.i("resoruces","exception");
e.printStackTrace();
}
xml=xml+"</ViewFlipper>\n</RelativeLayout>";
xml=xml+"<!--xxx-->";
try{
// Create file
FileWriter fstream = new FileWriter("res/layout/main.xml");
BufferedWriter out = new BufferedWriter(fstream);
out.write(xml);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
Log.i("xml write","exception");
e.printStackTrace();
}
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.adflipper);
Button btn=(Button)findViewById(R.id.restbutton);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.showNext();
}
});
}
}
我已经实现了代码,它可以使项目drawables相应地生成一个main.xml但它不起作用(我有一个例外:找不到文件res / layout / main.xml)。 (我的目的是根据不同数量的png图像生成main.xml)我做错了什么,是否有可能克服这个问题还是有其他解决方案?
感谢。
答案 0 :(得分:3)
您可以动态更改ImageView的src,而无需生成XML。
ImageView imageView = (ImageView) findViewById(R.id.your_image_view);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_png));
您可以通过动态创建并添加到父RelativeLayout来添加更多内容:
for (int i=0x7f020000 ;i<0x7f020020;i++)
ImageView imageView = new ImageView(this);
imageview.setBackgroundResource(R.drawable.your_png_reference);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.your_relative_layout_id);
ll.addView(imageView);
}