看板 AndroidDev 關於我們 聯絡資訊
※ 引述《neko0624 (Neko)》之銘言: : 請問 : 在Activity中有setContentView跟addContentView兩種方法 : 那有沒有能移除ContentView的方法或是技巧呢 : 我用addContentView加了好幾層的View上去 : 現在要移除掉幾個已經加上去的View 推文太麻煩 直接寫一隻程式給你參考 package com.givemepass.TestContentView; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class TestContentView extends Activity { private LinearLayout linearlLayout; private Button button; private TextView [] textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); //linearlLayout = (LinearLayout)findViewById(R.id.linearLayout01); linearlLayout = new LinearLayout(this); linearlLayout.setOrientation(LinearLayout.VERTICAL); setContentView(linearlLayout); button = new Button(this); textView = new TextView[10]; linearlLayout.addView(button); for(int i=0;i<10;i++){ textView[i] = new TextView(this); textView[i].setText("TextView"+i); linearlLayout.addView(textView[i]); } button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(linearlLayout.getChildCount()!=1){ linearlLayout.removeViewAt( linearlLayout.getChildCount()-1); } } }); } } 我試過了 可以動 直接copy paste就可以了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.221.115.4
neko0624:感謝 我來研究一下 08/03 15:42
neko0624:讚 今天釐清不少觀念 光是Android的View就可以研究半年了 08/03 15:59
caty1010:看到此文,受益良多!! 08/03 16:45
neko0624:問題解決了 感謝大大 08/05 13:32