android中动态添加组件

</pre><pre name="code" class="html">
public void choices_add(View v){
		if(choicesNumber>2){
		ImageView choice_ImageView=(ImageView)findViewById(choicesNumber+100);
		choice_ImageView.setVisibility(View.INVISIBLE);	
		}
		
		++choicesNumber;//添加choices的个数
		RelativeLayout new_choice_relativeLayout=getRelativeLayout();
		LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		choices_linearLayout.addView(new_choice_relativeLayout, LP_FW);
	}
	public RelativeLayout getRelativeLayout(){
		RelativeLayout new_choice_relativeLayout=new RelativeLayout(this);
		new_choice_relativeLayout.setId(choicesNumber);
		//添加TextView
		TextView tv = new TextView(this);
		RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数
		RL_WW.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		tv.setLayoutParams(RL_WW);
		tv.setPadding(10, 0, 0, 0);
		tv.setText(choicesNumber+".");
		tv.setTextColor(Color.rgb(0xa0, 0x00, 0x00));
		tv.setTextSize(20);
		new_choice_relativeLayout.addView(tv);
		//添加EditText
		EditText editText=new EditText(this);
		RelativeLayout.LayoutParams RL_FW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		RL_FW.setMargins(30, 0, 30, 0);
		editText.setLayoutParams(RL_FW);
		new_choice_relativeLayout.addView(editText);
	
		//添加imageView
		final ImageView imageView=new ImageView(this);
		imageView.setId(choicesNumber+100);
			RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
			RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			imageView.setPadding(0, 0, 3, 0);
			imageView.setLayoutParams(RL_WW);
			imageView.setImageResource(R.drawable.ic_del);
			new_choice_relativeLayout.addView(imageView);	
			imageView.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					System.out.println("点击的imageView的ID号为:"+imageView.getId());
					RelativeLayout relativeLayout=(RelativeLayout)findViewById(choicesNumber);
					//relativeLayout.setVisibility(View.GONE);
					choices_linearLayout.removeView(relativeLayout);
					--choicesNumber;
					System.out.println("点击的imageView后choicesNumber为:"+choicesNumber);
					if(choicesNumber>2){
						ImageView choice_ImageView=(ImageView)findViewById(choicesNumber+100);
						choice_ImageView.setVisibility(View.VISIBLE);	
					}
				}
				
			});
			return new_choice_relativeLayout;
	}
      动态添加控件,然后通过ID号删除,但是删除函数如果用View.gone时会出现bug,但是用Remove就不会出错。具体原因请大家指正。

你可以通过以下步骤在Android动态添加桌面组件: 1. 创建小组件布局:首先,创建一个布局文件来定义小组件的外观。这可以在res/layout目录下的XML文件完成。你可以使用不同的视图和布局来设计小组件。 2. 创建小组件提供者:创建一个扩展AppWidgetProvider类的Java类。这个类将负责处理小组件的生命周期事件,例如创建、更新和删除小组件。 3. 配置小组件提供者:在AndroidManifest.xml文件,注册你的小组件提供者类。使用<receiver>标签来指定小组件提供者类,并使用<intent-filter>标签来定义处理小组件生命周期事件的操作。 4. 更新桌面:当用户将小组件拖放到桌面时,你需要在运行时动态添加组件。为此,你可以使用AppWidgetManager类的updateAppWidget()方法来更新小组件视图。 下面是一个示例代码,展示如何动态添加桌面组件: ```java // 创建小组件布局 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); // 配置小组件点击事件 Intent intent = new Intent(context, MyWidgetProvider.class); intent.setAction("ACTION_WIDGET_CLICKED"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.widget_button, pendingIntent); // 更新桌面组件 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName componentName = new ComponentName(context, MyWidgetProvider.class); appWidgetManager.updateAppWidget(componentName, remoteViews); ``` 请注意,你需要根据你的需求适应以上代码,并根据你的小组件布局和逻辑进行修改。 希望这能帮助到你!如有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值