以编程方式显示所有可用布局

时间:2013-01-23 09:51:37

标签: android android-layout layout

我正在尝试以编程方式显示所有布局     实际上我可以在纯java中完成它而不是在android中

这是我的第一堂课:

public DiskFileExplorer(String path, Boolean subFolder) {
    super();
    this.initialpath = path;
    this.recursivePath = subFolder;
}

public String[] list() {
    return this.listDirectory(this.initialpath);
}

@SuppressWarnings("null")
private String [] listDirectory(String dir) {
    String[] values = null;
    File file = new File(dir);
    File[] files = file.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory() == true) {
                //Log.v("Dossier" , files[i].getAbsolutePath());
                this.dircount++;
            } 
            else {
                //Log.v("Fichier" ,files[i].getName());
                values[i] = files[i].getName();
                this.filecount++;
            }
            if (files[i].isDirectory() == true && this.recursivePath == true) {
                this.listDirectory(files[i].getAbsolutePath());
            }
        }
    }
    return values;
}

和第二个:

public class MainActivity extends Activity {
    public String []DiskFileExplorer(String path, Boolean subFolder){
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.mylist);
        String pathToExplore = "./res/layout";
        DiskFileExplorer d_expl = new DiskFileExplorer(pathToExplore,true);
        String[] values = d_expl.list();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter); 
    }
}

和我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout> 

和eveentually纯java类,可以做我想在android中做的事情:

import java.io.File;

/**
 * Lister le contenu d'un répertoire
 * @author fobec 2010
 */
public class DiskFileExplorer {

    private String initialpath = " ";
    private Boolean recursivePath = false;
    public int filecount = 0;
    public int dircount = 0;

   /**
    * Constructeur
    * @param path chemin du répertoire
    * @param subFolder analyse des sous dossiers
    */
    public DiskFileExplorer(String path, Boolean subFolder) {
        super();
        this.initialpath = path;
        this.recursivePath = subFolder;
    }

    public void list() {
        this.listDirectory(this.initialpath);
    }

    private  void listDirectory(String dir) {
        File file = new File(dir);
        File[] files = file.listFiles();

        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory() == true) {
                    System.out.println("Dossier" + files[i].getAbsolutePath());
                    this.dircount++;
                } else {
                    System.out.println("" + files[i].getName());

                    this.filecount++;
                }
                if (files[i].isDirectory() == true && this.recursivePath == true) {
                    this.listDirectory(files[i].getAbsolutePath());
                }
            }
        }
    }

    /**
     * Exemple : lister les fichiers dans tous les sous-dossiers
     * @param args
     */
    public static void main(String[] args) {
        String pathToExplore = "C:/Users/R_nkusi/workspace/Copy of AppList_bis/res/layout";
        DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, true);
        Long start = System.currentTimeMillis();

        diskFileExplorer.list();

        System.out.println("----------");
        System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
        System.out.println(diskFileExplorer.dircount + " dossiers");
        System.out.println(diskFileExplorer.filecount + " fichiers");
    }
}

你必须改变“pathToExplore”并给出你自己的计算机中存在的那个。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你可以读出你自己的XML布局文件,但你无法在你的文件系统中找到它,因为它都是编译的,因此不是人类可读的。

获取所有布局资源ID及其实际名称:

void getIds() {
    Field[] ID_Fields = R.layout.class.getFields();
    int[] resArray = new int[ID_Fields.length];
    for(int i = 0; i < ID_Fields.length; i++)
        try {
            resArray[i] = ID_Fields[i].getInt(null);
            System.out.println(resArray[i] + " : " + getResources().getResourceEntryName(resArray[i]));
            getEventsFromAnXML(getResources().getXml(resArray[i])); //I'll show this later
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
}

但是,您需要自己解析XML。这是一个小例子:

private String getEventsFromAnXML(XmlResourceParser xpp) throws XmlPullParserException, IOException {
    StringBuffer stringBuffer = new StringBuffer();
    xpp.next();
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if(eventType == XmlPullParser.START_DOCUMENT) {
            stringBuffer.append("--- Start XML ---");
        }
        else if(eventType == XmlPullParser.START_TAG) {
            stringBuffer.append("\nSTART_TAG: "+xpp.getName());
        }
        else if(eventType == XmlPullParser.END_TAG) {
            stringBuffer.append("\nEND_TAG: "+xpp.getName());
        }
        else if(eventType == XmlPullParser.TEXT) {
            stringBuffer.append("\nTEXT: "+xpp.getText());
        }
        eventType = xpp.next();
    }
    stringBuffer.append("\n--- End XML ---");
    System.out.println(stringBuffer.toString());
    return stringBuffer.toString();
}

我希望这些代码可以帮助您到达目的地。

new AlertDialog.Builder(this)
.setTitle("Seen?")
.setMessage("Some message.")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // ok
    }
 })
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // no
    }
 })
 .show();