1

私はアンドロイド開発に不慣れで、テストコードの問題に直面しています。「+」ボタンをクリックしたときにボタンを追加したい。追加されていますが、位置が正しくありません。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 個のボタンでは機能しませんが、手元に表示されるボタンの数がわかっている場合は機能します

-TableRowの後に使用しEditText、動的にボタンを作成した後に に追加しますTableRow

-ボタンがエミュレーターまたは電話で画面に表示できる範囲を超える場合、ボタンが画面の右側を通過しないように注意してください

于 2012-09-19T06:09:50.333 に答える