我是 android 开发的新手,我的测试代码面临问题。单击“+”按钮时,我想添加按钮。它被添加但位置不正确。我想在编辑通过 XML 定义的文本后添加新按钮。我正在添加 xml 和我的代码。谢谢。
<RelativeLayout 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:background="#EBE7E4" >
<RelativeLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@layout/header_gradient"
android:gravity="right"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="false"
android:layout_centerVertical="true"
android:layout_marginLeft="18dp"
android:src="@drawable/content_new" />
</RelativeLayout>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/header"
android:layout_marginLeft="36dp"
android:ems="10"
android:hint="Name of label" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1" />
<LinearLayout
android:id="@+id/lId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_centerVertical="true"
android:layout_marginLeft="33dp"
android:text="Save & Continue" />
private Spinner spinner;
ImageButton btnAddNew;
public static int MY_BUTTON = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_steps);
btnAddNew =(ImageButton)findViewById(R.id.imageButton1);
addItemsOnSpinner();
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + MY_BUTTON);
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
Button newPass = new Button(getApplicationContext());
newPass.setText("Add LABEL");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(418);
newPass.setId(MY_BUTTON);
//newPass.
newPass.setOnClickListener(this);
rAlign.addView(newPass);
MY_BUTTON ++;
}
});
}