解决方案 »
- 求思路,怎么获取android SDK中所有的类名和方法名
- 求 Funambol android 客户端demo
- android模拟器启动停留于一画面 Waiting for HOME ('android.process.acore') to be launched...
- android如何使用syncML来同步通讯录
- andengine中如何检测矩形和线段的碰撞
- 华为C8650如何刷机
- android2.3 手机没法播放3gp文件!!!
- 在wrox网站上下载来的android源码 如何在 eclipse中运行
- 高手请帮忙:关于动态生成编辑框的问题?
- 如何在data/data/PackageName/files/子文件夹/文件.xml 存多层目录?
- android一个activity如何通过判定跳转到另外的activity
- 新手求解~
写成工具类 :public class XmlUtil {
public View XmlToView(String xml) {
//TODO
return view;
}
}
写成工具类 :public class XmlUtil {
public View XmlToView(String xml) {
//TODO
return view;
}
}
大神,能不能具体点,我就是卡在这里了,我不是那种直接要代码的人,给个链接也行。求指点...
写成工具类 :public class XmlUtil {
public View XmlToView(String xml) {
//TODO
return view;
}
}
大神,能不能具体点,我就是卡在这里了,我不是那种直接要代码的人,给个链接也行。求指点...
我也想过这么做,但是卡着动不了。
贴一段你们定义的XML,我看看有没有什么思路.
private void createEditView(String _text,String _width,String _height,String _ID,String _ischageLine,String _parentID)
{
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(Integer.parseInt(_ischageLine)>0)
//换行标志,进行换行,如果大于0进行换行
lp.addRule(RelativeLayout.BELOW, Integer.parseInt(_parentID));
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); EditText editTxt=new EditText(this);
editTxt.setWidth(Integer.parseInt(_width));
editTxt.setHeight(Integer.parseInt(_height));
editTxt.setId(Integer.parseInt(_ID));
rl.addView(editTxt, lp);
}
/**
* 创建Button 控件
* @param _text
* @param _width
* @param _height
* @param _ID
* @param _ischageLine
* @param _parentID
*/
private void createButton(String _text,String _width,String _height,String _ID,String _ischageLine,String _parentID)
{
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(Integer.parseInt(_ischageLine)>0)
//换行标志,进行换行,如果大于0进行换行
lp.addRule(RelativeLayout.BELOW, Integer.parseInt(_parentID));
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); Button btn=new Button(this);
btn.setWidth(Integer.parseInt(_width));
btn.setHeight(Integer.parseInt(_height));
btn.setText(_text);
btn.setId(Integer.parseInt(_ID));
btn.setOnClickListener(new MyOnClickListener());//注册事件
rl.addView(btn, lp);
}这是主要功能,问题就是把这两个的创建方法合二为一就好了,不知道行不行,因为还要创建很多不同的控件。 通过赌徒XML的节点While这两个方法。
没有XML布局文件,是通过代码创建控件。
<control>
<!--type/控件名称(必须有一定的规范性,非空),id/每个控件的ID号(非空),text/控件显示的文本
width/宽dp ,height/高dp,ischageLine/是否需要换行,chageParentID/在某个元素下方的ID-->
<item type="TextView" id="1" text="手机号码:" width="60" height="20" ischageLine="0" />
<item type="EditText" id="2" text="" width="200" height="20" ischageLine="0" />
<item type="TextView" id="3" text="充值地区:" width="60" height="20" ischageLine="1" chageParentID="2"/>
<item type="EditText" id="4" text="" width="200" height="20" ischageLine="1" chageParentID="2"/>
<item type="TextView" id="5" text="充值金额:" width="60" height="20" ischageLine="1" chageParentID="4"/>
<item type="EditText" id="6" text="" width="200" height="20" ischageLine="1" chageParentID="4"/>
<item type="Button" id="7" text="下一步" width="100" height="20" ischageLine="1" chageParentID="6"/>
</control>这个事外部定义的XML,通过这个创建控件。
根据你的意思,我就只差把他写成工具类了,把那些属性定义成实体对象,这些我都能理解,“此VIEW对象可能是已经包含了child的VIEW” 这个不太理解??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String xml = "<control>"+
"<item type=\"TextView\" id=\"1\" text=\"手机号码:\" width=\"60\" height=\"40\" ischageLine=\"0\" />" +
"<item type=\"EditText\" id=\"2\" text=\"\" width=\"200\" height=\"40\" ischageLine=\"0\" />" +
"<item type=\"TextView\" id=\"3\" text=\"充值地区:\" width=\"60\" height=\"40\" ischageLine=\"1\" chageParentID=\"2\"/>" +
"<item type=\"EditText\" id=\"4\" text=\"\" width=\"200\" height=\"40\" ischageLine=\"1\" chageParentID=\"2\"/>" +
"<item type=\"TextView\" id=\"5\" text=\"充值金额:\" width=\"60\" height=\"40\" ischageLine=\"1\" chageParentID=\"4\"/>" +
"<item type=\"EditText\" id=\"6\" text=\"\" width=\"200\" height=\"40\" ischageLine=\"1\" chageParentID=\"4\"/>" +
"<item type=\"Button\" id=\"7\" text=\"下一步\" width=\"100\" height=\"40\" ischageLine=\"1\" chageParentID=\"6\"/>" +
"</control>";
Control control = new Control();
control.setViewsFromXml(this, xml);
// 已经把XML转成了view的集合
List<View> views = control.getViews();
// 根据需要把集合显示到界面
LinearLayout layout = (LinearLayout) findViewById(R.id.line);
System.out.println(views.size());
for (View view : views) {
layout.addView(view);
}
}}
public class Control {
Context context;
private List<View> views = new ArrayList<View>();
public List<View> getViews() {
return views;
} public void setViews(List<View> views) {
this.views = views;
} public void setViewsFromXml(Context context, String xml) {
this.context = context;
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
XmlHandler handler = new XmlHandler();
try {
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new StringReader(xml)));
} catch (Exception e) {
e.printStackTrace();
}
}
class XmlHandler extends DefaultHandler {
private View view;
private String text;
private int id,width,height,ischageLine,chageParentID;
@Override
public void startDocument() throws SAXException {
super.startDocument();
} @Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String type = attributes.getValue("type");
if (type == null) {
return;
}
System.out.println("type-->" + type);
if (type.equals("TextView")) {
view = new TextView(context);
} else if (type.equals("EditText")) {
view = new EditText(context);
} else if (type.equals("Button")) {
view = new Button(context);
}
String widthStr = attributes.getValue("width");
if (widthStr != null) {
width = Integer.parseInt(widthStr);
}
System.out.println("width-->" + widthStr);
String heightStr = attributes.getValue("height");
if (heightStr != null) {
height = Integer.parseInt(heightStr);
}
System.out.println("height-->" + height);
text = attributes.getValue("text");
System.out.println("text-->" + text);
} @Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("Item")) {
LayoutParams params = new LayoutParams(width, height);
view.setLayoutParams(params);
if (view instanceof TextView) {
TextView t = (TextView) view;
t.setText(text);
// ....
} else if (view instanceof EditText) {
EditText t = (EditText) view;
t.setText(text);
} else if (view instanceof Button) {
Button t = (Button) view;
t.setText(text);
}
views.add(view);
}
} @Override
public void characters(char[] ch, int start, int length)
throws SAXException {
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/line"
android:orientation="vertical"
></LinearLayout>
谢谢,因为刚学Android,不知道还可以这样做,再一次膜拜,结贴了。哈哈