android中如何删除布局中的控件
来源:互联网 发布:淘宝主图白底图要求 编辑:程序博客网 时间:2024/05/30 19:34
内容来自: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; } }}----------------------------------------------------------------------------------
- android中如何删除布局中的控件
- 如何删除android中的控件
- Android 布局中 如何使控件居中
- Android 布局中如何使控件居中
- Android中如何在“自定义布局”中“动态”添加控件
- Android中的布局和控件
- 如何删除Composite控件中的子控件
- Android获取dialog自定义布局中的控件(从控件中获得参数)
- ExtJs布局中,控件如何水平居中?
- Android获取dialog自定义布局中的控件
- android 获取 控件在布局中的位置
- Android获取dialog自定义布局中的控件
- Android获取Dialog自定义布局中的控件
- Android获取dialog自定义布局中的控件
- 4.28如何在代码中改变控件在布局中的位置
- Android中TableLayout中的布局
- Android中TableLayout中的布局
- Android中TableLayout中的布局
- android中的httpdown的工具类
- 打印控件调查
- Lua package 使用
- 设计模式——状态,C# 语言范例
- Introduction to Topic Modeling learning(链)
- android中如何删除布局中的控件
- android中对sd卡的操作
- springMVC工作原理
- IOS自动化的打渠道包
- C的xml编程-libxml2
- 如何成为一名优秀的web前端工程师(前端攻城师)
- 世界上最有用的100个网站
- img标签与文字对齐方式
- 绝对蛋疼——论js中的submit函数