我刚到公司的时候看程序代码,怎么都找不到对应的布局文件,后来才发现他们是从assets中读取一篇xml文件动态的创建界面,可能我是个初学者的原因还什么都没怎么见过,对此感到特别的好奇所以研究了一下。
1首先准备了一篇xml文件放到了assets目录下,
<?xml version="1.0" encoding="UTF-8"?>
<root>
<assess_item title="评分项目" hide="true">
<spinner title="姓名:" >[],张三,李四</spinner>
<spinner >[],吃,喝,玩,乐</spinner>
<input title="犯罪前科:" >无</input>
<input title="既往史:" >无</input>
<input title="身高:" >未测,卧床</input>
<input title="体重:" >未测,卧床</input>
<spinner title="文化程度:" >小学,初中,高中,大专,本科以上</spinner>
<input title="饮酒:" />
<input title="脉搏:" />
<input title="呼吸:" />
<input title="收缩压:"/>
<input title="舒张压:" />
<spinner title="意识:" >清楚,模糊,谵妄,嗜睡,昏迷</spinner>
<spinner title="表情:" >正常,急性病容,慢性病容</spinner>
<spinner title="言语:" >正常,含糊不清,失语</spinner>
<input title="皮肤破损:" />
<spinner title="营养状况:" >好,中,差</spinner>
<spinner title="四肢:" >活动自如,功能障碍</spinner>
<input title="四肢异常:" />
<spinner title="感官功能:" >正常,异常</spinner>
<input title="感官异常:" />
<input title="排便异常:" />
<spinner title="静脉输液:" >无,有,表浅静脉,套管针,PICC管,大静脉置管</spinner>
<input title="输液部位:" />
<input title="其他"/>
</assess_item>
<!-- <no_danger/> -->
<!-- <export/> -->
</root>
最后加载完成的界面的效果。
添加布局文件主要的布局文件 activity_main.xml
<ScrollView 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" >
<LinearLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
上述代码就比较简单就是一个scrollView里边嵌套一个线性布局。
以下两个item,item.xml和item2.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="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_item_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="身高:" />
<Spinner
android:id="@+id/sp_item_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
</LinearLayout>
<?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="horizontal" >
<TextView
android:id="@+id/tv_item2_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="TextView:" />
<EditText
android:id="@+id/et_item2_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
2 获取这篇xml文件的输入流;
AssetManager assetManager = this.getAssets();
parser = Xml.newPullParser();
InputStream is = assetManager.open("NurseAssess_InHospital.xml",
AssetManager.ACCESS_STREAMING);//按顺序读取
这里用到了一个assetManger用这个类的open方法得到这篇xml文件的输入流。
给解析器设置输入流:
parser.setInput(is, "utf-8");
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (type == XmlPullParser.START_TAG) {
}
}
开始读取,
以下代码写在上边代码的if语句里边。
if (name.equals("spinner")) {
String tag = parser.getAttributeValue(null, "title");
Log.i("TAG", "tag=" + tag);
inflater = getLayoutInflater();
view = inflater.inflate(R.layout.item, null);
textView = (TextView) view.findViewById(R.id.tv_item_1);
textView.setText(tag);
spinner = (Spinner) view.findViewById(R.id.sp_item_1);
String string = parser.nextText();
String[] strings = string.split(",");
// Log.i("TAG", "string="+string);
list = new ArrayList<String>();
for (String string2 : strings) {
Log.i("TAG", "string2" + string2);
list.add(string2);
}
Log.i("TAG", "strings=" + strings);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
ll_2.addView(view);
if (name.equals("input")) {
inflater = getLayoutInflater();
String string = parser.getAttributeValue(null, "title");
Log.i("TAG", "string=" + string);
View view = inflater.inflate(R.layout.item2, null);
et = (EditText) view.findViewById(R.id.et_item2_1);
textView2 = (TextView) view
.findViewById(R.id.tv_item2_1);
textView2.setText(string);
ll_2.clearFocus();
ll_2.setFocusable(true);
ll_2.setFocusableInTouchMode(true);
ll_2.addView(view);
}
以下是完整的MianActivity的代码:
package com.example.day1403;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private Spinner spinner;
private XmlPullParser parser;
private LayoutInflater inflater;
private ArrayAdapter<String> adapter;
private List<String> list;
private EditText et;
private LinearLayout ll_2;
private TextView textView;
private TextView textView2;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_2 = (LinearLayout) findViewById(R.id.ll_2);
getView();
}
private void getView() {
try {
AssetManager assetManager = this.getAssets();
parser = Xml.newPullParser();
Log.i("TAG", "执行了");
InputStream is = assetManager.open("NurseAssess_InHospital.xml",
AssetManager.ACCESS_STREAMING);
parser.setInput(is, "utf-8");
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
String name = parser.getName();
Log.i("TAG", "name=" + name);
if (type == XmlPullParser.START_TAG) {
if (name.equals("spinner")) {
String tag = parser.getAttributeValue(null, "title");
Log.i("TAG", "tag=" + tag);
inflater = getLayoutInflater();
view = inflater.inflate(R.layout.item, null);
textView = (TextView) view.findViewById(R.id.tv_item_1);
textView.setText(tag);
spinner = (Spinner) view.findViewById(R.id.sp_item_1);
String string = parser.nextText();
String[] strings = string.split(",");
// Log.i("TAG", "string="+string);
list = new ArrayList<String>();
for (String string2 : strings) {
Log.i("TAG", "string2" + string2);
list.add(string2);
}
Log.i("TAG", "strings=" + strings);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
ll_2.addView(view);
}
if (name.equals("input")) {
inflater = getLayoutInflater();
String string = parser.getAttributeValue(null, "title");
Log.i("TAG", "string=" + string);
View view = inflater.inflate(R.layout.item2, null);
et = (EditText) view.findViewById(R.id.et_item2_1);
textView2 = (TextView) view
.findViewById(R.id.tv_item2_1);
textView2.setText(string);
ll_2.clearFocus();
ll_2.setFocusable(true);
ll_2.setFocusableInTouchMode(true);
ll_2.addView(view);
}
}
}
is.close;
} catch (FileNotFoundException e) {
Log.i("TAG", "文件没找到");
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
因为我是新手难免有错误,希望有错误的地方大家积极指出谢谢