解决方案 »

  1.   

    这里的反射不同于程序里的反射..只不过是根据XML中的定义,动态的生成VIEW控件.androia是支持直接用java程序创建view对象的.因此你只要制定一系列的规则,然后根据你XML中的规则,来创建VIEW对象即可.
      

  2.   

    我在想,楼主是不是需要用layoutinflater
      

  3.   

          我通过这个方法已经完成了,但是创建每个控件的时候,方法太多。如:创建EditText时需要写个创建EditText的方法,创建TextView时就需要个创建EditText方法,请问大神有没有通过View创建子控件的??在线等
      

  4.   

     这个不是读取内部资源,而是用到XML文件,不是XML布局,肯能layoutinflater还用不到,或者说你有过类似的开发经验?求指教
      

  5.   


    写成工具类 :public class XmlUtil {
      public View XmlToView(String xml) {
        //TODO
        return view;
      }
    }
      

  6.   


    写成工具类 :public class XmlUtil {
      public View XmlToView(String xml) {
        //TODO
        return view;
      }
    }

    大神,能不能具体点,我就是卡在这里了,我不是那种直接要代码的人,给个链接也行。求指点...
      

  7.   


    写成工具类 :public class XmlUtil {
      public View XmlToView(String xml) {
        //TODO
        return view;
      }
    }

    大神,能不能具体点,我就是卡在这里了,我不是那种直接要代码的人,给个链接也行。求指点...
    我也想过这么做,但是卡着动不了。
      

  8.   

    大神不敢当
    贴一段你们定义的XML,我看看有没有什么思路.
      

  9.   


    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这两个方法。
      

  10.   


    没有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,通过这个创建控件。
      

  11.   

    我说下我的思路:首先,定义一个class,里面包含了xml里定义的一些属性,比如控件类型,控件大小,子控件  等等然后遍历XML,把XML变成实体对象然后根据这个实体对象,生成VIEW对象(此VIEW对象可能是已经包含了child的VIEW)然后把这个view 放到 activity 中去..此一系列方法可以写成公共方法..
      

  12.   


    根据你的意思,我就只差把他写成工具类了,把那些属性定义成实体对象,这些我都能理解,“此VIEW对象可能是已经包含了child的VIEW” 这个不太理解??
      

  13.   

    public class MainActivity extends Activity { @Override
    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>
      

  14.   


    谢谢,因为刚学Android,不知道还可以这样做,再一次膜拜,结贴了。哈哈