布局问题  我用xml来布局的应该可以实现的  但动态生成按钮 不知道怎么做?

解决方案 »

  1.   

    我布局样式http://pan.baidu.com/share/link?shareid=466970&uk=907568367
      

  2.   

    代码:
    RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.rootLayout);

    Button btnOK = new Button(this);
    btnOK.setText("OK");
    rootLayout.addView(btnOK);
    layout文件:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rootLayout"
        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" >    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" /></RelativeLayout>
      

  3.   

    一个原则,xml文件能做到的所有功能,代码必定也能做到。因为xml定义文件最终还是得解释成为对应控件(或其它资源)的实例对象,并加载到内存中才能显示出来。
      

  4.   

    用代码,得到id=rootLayout的RelativeLayout ,将button作为一个view用addView()方法加进去,刷新就好了。BTW:AppDemo里有这东西。