拉界面有两种方式啊,一种是XML,一种是写代码
XML方式简单方便,而且直观,把界面和功能分开,代码也更清晰,
但是也有不好的地方 1.很简单就可以被反编译出来  2.不能动态改变 (暂时只发现这两个)用XML拉出来的界面,代码完全可以实现,不过相对麻烦

解决方案 »

  1.   

    LS已经都说了,存在的目的就是用来UI和代码分离。
    举例而言,假设界面上有一个按钮,那么你可以在代码里面只写上这个按钮的响应事件及后续处理。至于这个按钮放在哪里?可以完全由XML配置掉。这样改变UI就很方便。例如我想把按钮的图片从黄色换成蓝色,那就改改XML就可以了。不过缺点也被说了,这样的话其实反编译就把你的layout搞定了。
      

  2.   

    如果是这样的话,你们看看这个简单程序
    package com.misoo.ex02;import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;public class ex02 extends Activity implements OnClickListener {
    @Override 
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    Button btn = (Button)findViewById(R.id.button);
    Button btn2 = (Button)findViewById(R.id.button2);
    btn.setOnClickListener(this);
    btn2.setOnClickListener(this);
    } public void onClick(View arg0) {
    switch (arg0.getId()) {
    case R.id.button:
    setTitle("this is OK button");
    break;
    case R.id.button2:
    this.finish();
    break;
    }
    }
    }
    main.xml如下:<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    />
    <Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK"
    />
    <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Exit"
    />
    </LinearLayout>
    我感觉这就是如二楼所说,按钮响应事件定义在主程序里,ui定义在xml里,但为什么没使用LayoutInflater呢?
      

  3.   

    这个代码我并没有跟进去过。
    不过我推测,在setContentView和findViewById内部,应该是用到了和LayoutInflater相关的代码的。
      

  4.   

    因为setContentView(); 
    3楼使用的UI是main.xml,所以setContentView的参数是R.layout.main, 这是唯一标识XML的一个常数,就代表了那个xml,所以没有必要用LayoutInflater而顶楼的第一段代码
    setContentView(layout1); 参数layout1不是一个XML文件,是你自己定义的一个UI(它没有对应的XML),所以你需要用LayoutInflater来构造这个UI
      

  5.   

    稍微翻了一下源代码:
    public class Activity extends ContextThemeWrapper
            implements LayoutInflater.Factory,
            Window.Callback, KeyEvent.Callback,
            OnCreateContextMenuListener, ComponentCallbacks这个是Activity的部分头。可以看到“LayoutInflater.Factory”,再往下应该可以找到相关的代码,所以我觉得,应该最终还是用了LayoutInflater,不过Activity为了使用简便,简化了调用逻辑。
      

  6.   

    而LayoutInflater的更多的用处,从API可以看出一些端倪 --- 例如clone。