0

2つのボタンでmainLayoutを作成しました

追加:他のレイアウトを追加します

remove:他のレイアウトを削除します。

    <Button
             android:id="@+id/btnAdd"
             android:textStyle="bold"

             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Add View"
             android:onClick="addView" />

      <Button
             android:id="@+id/btnRemove"
             android:textStyle="bold"

             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Remove View"
             android:onClick="removeView" />

今、私はaddViewボタンをクリックしたときにビューを追加するために次のコードを書きました

               LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    view=inflater.inflate(R.layout.other_layout,null);


    mainLayout.addView(view);

ビューはメインレイアウトの下に追加されます。ただし、画面の下部ではなく、画面の中央にビューを追加する必要があります。

どうやってやるの?

4

5 に答える 5

0

この行を変更して、親ビューを配置します。

view=inflater.inflate(R.layout.other_layout,PARENT_VIEW_OBJECT);

それはうまくいくはずです

于 2012-07-11T04:43:22.523 に答える
0

メインレイアウトの上部に空の LinearLayout プレースホルダーを配置し、メインレイアウトの代わりにビューを追加します。

最初にプレースホルダーを調整して、その場所と外観を変更できます。

<LinearLayout
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
..../>

<Button
..../>
于 2016-02-25T20:22:23.883 に答える
0

このようにparentLayoutを取得します

parentLayout=(YourParentLayout)view.findViewById(R.id.parentLayout);

それから

parentLayout.addView(yourview);
于 2012-07-11T04:51:18.630 に答える
0

以下のコードを確認できますが、私にとっては正常に機能しています

private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;

inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);

parent = (ViewGroup) findViewById(R.id.mainxml);


addbutton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           view = inflater.inflate(R.layout.other, null);
            parent.addView(view);
       }
   });
于 2012-07-11T04:57:12.853 に答える