解析xml文件动态加载用户界面

来源:互联网 发布:mysql入门很简单 视频 编辑:程序博客网 时间:2024/06/06 07:40
    package com.example.dynamic;

未解决,提供思路



import java.io.IOException;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class DynamicActivity extends Activity {
LinearLayout layout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
Button button = new Button(this);
button.setText("按钮");
button.setTextColor(Color.RED);
button.setBackgroundResource(R.drawable.ic_launcher);
button.setLayoutParams(new LayoutParams(100,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
button.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View arg0) {
// TextView textView = new TextView(DynamicActivity.this);
// textView.setText("点击了按钮");
// layout.addView(textView);
new Thread(new Runnable() {


@Override
public void run() {
try {
// 得到輸入流
InputStream is = getAssets().open("tv_main.xml");
// 解析Xml文件
try {
XmlPullParserFactory xl = XmlPullParserFactory
.newInstance();
XmlPullParser xpp = xl.newPullParser();
xpp.setInput(is, "UTF-8");
// 得到下一个事件
// int eventType = xpp.next();
int eventType;
// 得到当前的事件
eventType = xpp.getEventType();
System.out.println("eventType" + eventType);
System.out.println(" XmlPullParser.START_TAG"
+ XmlPullParser.START_TAG);
StringBuffer sb = new StringBuffer();
// todo
// 循环事件
while (eventType != XmlPullParser.END_DOCUMENT) {


switch (eventType) {


case XmlPullParser.START_TAG: {
// System.out.println("sssss");
String tag = xpp.getName();
if (tag.equalsIgnoreCase("LinearLayout")) {
// xpp.getAttributeValue(0);
if (xpp.getDepth() == 0) {
LinearLayout linearLayoutMain = new LinearLayout(
DynamicActivity.this);



} else if (xpp.getDepth() == 1) {


} else if (xpp.getDepth() == 3) {


} else {
}


} else if (tag
.equalsIgnoreCase("ScrollView")) {


} else if (tag
.equalsIgnoreCase("RelativeLayout")) {


} else if (tag
.equalsIgnoreCase("Button")) {
// Button button2=new Button(this);


}
break;
}


case XmlPullParser.TEXT: {
Log.v("Text:", xpp.getText() + "TEXT");
break;
}


default: {
System.out.println("default");
}


}
// 获取下一个事件
eventType = xpp.next();
}

} catch (XmlPullParserException e) {
e.printStackTrace();
}

// Properties properties=new Properties();
// properties.load(is);
// Set<String> set =
// properties.stringPropertyNames();
// Iterator<String> it = set.iterator();
// String yu = (String)
// properties.get("LinearLayout");
// System.out.println(""+yu);
// Set<Object> set1 = properties.keySet();
// Iterator<Object> it1 = set1.iterator();
// while(it.hasNext()){
// Log.v("Set", it.next());
// }
// while(it1.hasNext()){
// Log.v("Set1", ""+it1.next());
// }
// 配置文件
// Log.v("oncreate",
// "LinearLayout"+properties.get("LinearLayout"));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
// layout.addView(button);
// setContentView(layout);
// getResources().getXml(R.id.action_settings);


}
}
原创粉丝点击