android 에서 레이아웃 의 컨트롤 을 삭제 하 는 방법

내용:http://zhidao.baidu.com/question/274313012.html
        ,       ,         ~

          ~   remove         ~

   layout    :
----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:id="@+id/linearlayout">


<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="wrap_content" android:layout_height="wrap_content">
 <Button android:layout_height="wrap_content" android:id="@+id/add"
  android:text="Add" android:layout_width="100px"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="100px" android:text="Remove" android:id="@+id/remove"></Button>
</LinearLayout>
<TextView android:id="@+id/TextView01" android:text="This is textView."
 android:layout_width="fill_parent" android:gravity="center"
 android:layout_height="50px"></TextView>

</LinearLayout>
----------------------------------------------------------------------------------

  Activity   :
----------------------------------------------------------------------------------
package com.foxconn.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class DialogTest extends Activity implements OnClickListener {

    private Button add_btn, remove_btn;
    private LinearLayout linearLayout;
    private int index = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        register();
    }

    private void register() {
        add_btn.setOnClickListener(this);
        remove_btn.setOnClickListener(this);
    }

    private void findViews() {
        add_btn = (Button) findViewById(R.id.add);
        remove_btn = (Button) findViewById(R.id.remove);
        linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
    }

    protected View createView() {
        Button btn = new Button(this);
        btn.setId(index++);
        btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btn.setText("aaaaaa" + index);
        return btn;
    }

    private void removeView() {
        //  linearlayout view   
        int count = linearLayout.getChildCount();
        //    LAYOUT  , 0    add remove  button layout
        // count-1          textview
        //  , remove   ,      0<location<count-1     
        //     remove ,   count-2    textview           ~
        if (count - 2 > 0) {
            //count-2>0      linearlayout view   2 ,      add   button
            linearLayout.removeViewAt(count - 2);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add:
            linearLayout.addView(createView(), 1);
            break;
        case R.id.remove:
            removeView();
            break;
        default:
            break;
        }
    }
}
----------------------------------------------------------------------------------

좋은 웹페이지 즐겨찾기