1

我是 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 &amp; 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 ++;

        }
    });
}   
4

2 回答 2

2

在这里,我通过更改 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: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"/>
</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:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/spinner1"
    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_below="@+id/lId"
    android:layout_centerVertical="true"
    android:layout_marginLeft="33dp"
    android:text="Save &amp; Continue" />
</RelativeLayout>

onCreate功能没有任何变化

于 2012-09-19T06:29:15.430 回答
0

-尽管此解决方案不适用于 N 个按钮,但如果您知道手头有多少个按钮,则可以使用

-使用TableRowafter EditText,然后在动态创建按钮后将它们添加到TableRow.

-当按钮超过屏幕在模拟器或手机中可以显示时,请注意不要通过屏幕右侧

于 2012-09-19T06:09:50.333 回答