3

EditText と 2 つのボタンを含む XML レイアウトがあります。プラスボタンをクリックすると、新しい編集テキストがプログラムで追加されます。これは機能しますが、編集テキストは異なって見えます。XMLによると、XMLで定義されたedittextには特別な属性はありませんので、特定のレイアウト設定ではないと思います。

私の質問は、プログラムで追加された EditText を同じように見せるにはどうすればよいですか?

数字を含む EditText は、プログラムで追加された edittext です。空のものは XML で作成しています。

スクリーンショット
(出典: tozz.nl )

コード:

        LinearLayout baseLayout = (LinearLayout) findViewById(R.id.baseLayout);

        LinearLayout linearLayout = new LinearLayout(getApplicationContext());
        linearLayout.setId(100 + numPlayers);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        EditText editText = new EditText(getApplicationContext());
        editText.setText(editText.toString().substring(25, 30));

        ImageButton delButton = new ImageButton(getApplicationContext());
        delButton.setImageResource(R.drawable.ic_delete);

        linearLayout.addView(editText);
        linearLayout.addView(delButton);

        baseLayout.addView(linearLayout);

私のXMLは次のとおりです。

    <LinearLayout
        android:id="@+id/linearPlayer1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editPlayer1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center_vertical" />



        <ImageButton
            android:id="@+id/addPlayer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_input_add" />

    </LinearLayout>
4

2 に答える 2

5

Luksprog は私の質問に次のように答えました。

新しいビューを作成するときに、アプリケーション コンテキストではなく、アクティビティ コンテキストを渡します。

于 2012-12-10T09:28:09.190 に答える
3

これらのビューを正しいもので追加すると、レイアウトの最初のビューのようになりますLayoutParamsEditText

    linearLayout.addView(editText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
    linearLayout.addView(delButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    baseLayout.addView(linearLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
于 2012-12-09T17:50:39.860 に答える