android --动态添加控件
来源:互联网 发布:公务员自动听课软件 编辑:程序博客网 时间:2024/05/24 06:35
用android做东西时需要用到在程序运行时动态添加控件,自己看了下别人的东西,然后结合自己的情况写了下,用着还行,
下面是代码
package com.example.addkj;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnLongClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;public class MainActivity extends Activity {LinearLayout MyLayout = null;View CurView = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {MyLayout = new LinearLayout(this);MyLayout.setOrientation(LinearLayout.VERTICAL);super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);for(int i=0;i<5;++i){Button btn01 = new Button(this);btn01.setText("Button "+i);MyLayout.addView(btn01);}setContentView(MyLayout);/*btn01.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {EditText te = new EditText(v.getContext());te.setBackgroundDrawable(null); //设置edittext没有边框,类似于android:background="@null"te.setText("新开出来的。。。"+te.getBaseline());MyLayout.addView(te);}});btn01.setOnLongClickListener(new OnLongClickListener(){@Overridepublic boolean onLongClick(View v) {CurView = v;return false;}});*/super.registerForContextMenu(MyLayout);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overridepublic boolean onContextItemSelected(MenuItem item) {switch(item.getItemId()){case Menu.FIRST:MyLayout.removeView(CurView);break;}return super.onContextItemSelected(item);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);//super.getMenuInflater().inflate(R.menu.mymenu, menu);menu.add(0,Menu.FIRST,1,"删除");menu.add(0,Menu.FIRST+2,2,"取消");}}
- Android 动态添加控件
- Android--动态添加控件
- Android 动态添加控件
- android --动态添加控件
- android 动态添加控件
- Android 动态代码添加控件
- android 动态设置控件的大小 和 动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- android 在布局中动态添加控件
- Android动态添加控件约束位置
- Android根据布局文件动态添加控件
- android代码中动态添加控件
- android 动态添加控件控制属性问题
- android中怎么动态添加TextView控件
- 【wordpress】基础教程
- 黑马程序员——Java 异常的捕获及处理
- 促进你开发技能的8个技巧
- MySQL 字符编码问题详细解释
- poj3693 Maximum repetition substring
- android --动态添加控件
- 精通css(7)-实例
- centos 6.x 安装chrome
- Java开发者的十大戒律
- UVA - 104 Arbitrage
- Yii学习指南(改编)3--模型-视图-控制器 (MVC)
- 解决内外网无法同时使用
- 再次深入了解epoll
- android--代码实现手机截屏