- Home ›
- Android入門 ›
- FrameLayoutクラス ›
- HERE
全ての子ビューを削除
広告
追加されている全ての子ビューを削除する方法を確認します。「FrameLayout」クラスの親クラスである「ViewGroup」クラスで用意されている「removeAllViews」メソッドを使います。
removeAllViews public void removeAllViews()
Call this method to remove all child views from the ViewGroup.
メソッドを実行すると全ての子ビューを削除します。
具体的には次のように記述します。
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); FrameLayout frameLayout = new FrameLayout(this); setContentView(frameLayout); Button button = new Button(this); button.setText("Button"); frameLayout.addView(button, new ViewGroup.LayoutParams(WC, WC)); TextView text = new TextView(this); text.setText("TextView"); text.setTextColor(Color.RED); frameLayout.addView(text, new ViewGroup.LayoutParams(WC, WC)); frameLayout.removeAllViews(0); }
サンプルプログラム
それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。
package jp.javadrive.android; import android.app.Activity; import android.os.Bundle; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.Button; import android.widget.TextView; import android.view.View; import android.view.ViewGroup; import android.graphics.Color; import android.view.View.OnClickListener; public class Test06_01 extends Activity implements OnClickListener { private final int FP = ViewGroup.LayoutParams.FILL_PARENT; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private Button buttonText; private Button buttonButton; private Button buttonClear; private FrameLayout frameLayout; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); setContentView(linearLayout); LinearLayout btnLinearLayout = new LinearLayout(this); btnLinearLayout.setOrientation(LinearLayout.HORIZONTAL); buttonText = new Button(this); buttonText.setText("Text"); buttonText.setOnClickListener(this); buttonButton = new Button(this); buttonButton.setText("Button"); buttonButton.setOnClickListener(this); buttonClear = new Button(this); buttonClear.setText("All Clear"); buttonClear.setOnClickListener(this); btnLinearLayout.addView(buttonText, createParam(WC, WC)); btnLinearLayout.addView(buttonButton, createParam(WC, WC)); btnLinearLayout.addView(buttonClear, createParam(WC, WC)); frameLayout = new FrameLayout(this); linearLayout.addView(btnLinearLayout, createParam(FP, WC)); linearLayout.addView(frameLayout, createParam(WC, WC)); } private LinearLayout.LayoutParams createParam(int w, int h){ return new LinearLayout.LayoutParams(w, h); } private ViewGroup.LayoutParams createViewGroupParam(int w, int h){ return new ViewGroup.LayoutParams(w, h); } public void onClick(View v) { if (v == buttonText){ TextView text = new TextView(this); text.setText("TextView"); text.setTextColor(Color.RED); frameLayout.addView(text, createViewGroupParam(WC, WC)); }else if (v == buttonButton){ Button button = new Button(this); button.setText("Button"); frameLayout.addView(button, createViewGroupParam(WC, WC)); }else if (v == buttonClear){ frameLayout.removeAllViews(); } } }
ビルド後にエミュレーター上で実行します。
「Text」ボタン又は「Button」ボタンをクリックするとFrameLayoutにビューが追加されていきます。
「All Clear」ボタンをクリックすると子ビューが全て削除されます。
( Written by Tatsuo Ikura )