package test.run;import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Button button = new Button(this);
button.setText("确定");
button.setHeight(50);
button.setWidth(50); LayoutParams lpRef = new LayoutParams(50,
50);
this.addContentView(button, lpRef);
}
}<?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/textView1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"/>
</LinearLayout>代码如上,我想运行时我创建的按钮是在TextView的下面,该如何写程序啊?

解决方案 »

  1.   

    setContentView(R.layout.main);
     要求:这句代码要保留,也就是使用原来的布局
      

  2.   

    实例化LinearLayout,LinearLayout.addView()
      

  3.   

    楼上的前辈,,,,如果实例化LinearLayout这个,,那是不是XML中的就作废了啊??
    我还想使用原来的XML中的布局,想加点东西进去
      

  4.   

    用ViewGroup,可以任意的动态生成按钮,方框等等,而且位置可以精确定位。