开发者

generate main.xml inside onCreate()

Is it possible to generate a main.xml inside onCreate()? Such as:

开发者_C百科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();
          } 
        }); 
      } 
    }

I have implemented the code and it can get the project drawables to generate a main.xml accordingly but its not working (I got an exception: file not found res/layout/main.xml). (My aim is to generate main.xml according to varying number of png images) What am I doing wrong, Is it possible to get over this or Is there any other solution?

Thanks.


You can change the src of your ImageView dynamically without having to generate the XML.

ImageView imageView = (ImageView) findViewById(R.id.your_image_view);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_png));

And you can add more by creating them dynamically and adding them to the parent 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜