android组件动态添加

本人最近开发一款订餐应用,有一个是关于订餐数量的,本来打算用的是listview,但是那里的listview没有学会,在网上也没有找到好的代码,于是博主最近就直接用的动态添加组件了,跟大家分享一下,一般情况下如果是动态添加组件,首先我们可以在最原始xml文件中有一个最基本的布局,比如是linelayout此时我们可以可以在java代码中获取这个布局的id值
private LinearLayout shopinfo_content;shopinfo_content=(LinearLayout)findViewById(R.id.shopinfo_content);
在得到id后,比如我用的foods之中存储用户所买的所有数据,那么我们就可以动态生成一个相当于listview的表格了,
private void setshop(){
		shopinfo_content.removeAllViews();//移除所有组件  用以界面刷新
		LinearLayout line=new LinearLayout(ShoppingActivity.this);
		line.setClickable(true);
		line.setOrientation(LinearLayout.HORIZONTAL);
		final TextView shop_name=new TextView(ShoppingActivity.this);//名称
		shop_name.setText("名称");
		shop_name.setTextSize(18);
		shop_name.setTextColor(Color.BLACK);
		line.addView(shop_name);
		final TextView shop_price=new TextView(ShoppingActivity.this);//单价
		shop_price.setText("                单价");
		shop_price.setTextSize(18);
		shop_price.setTextColor(Color.BLACK);
		line.addView(shop_price);
		final TextView shop_number=new TextView(ShoppingActivity.this);//数量
		shop_number.setText("         数量");
		shop_number.setTextSize(18);
		shop_number.setTextColor(Color.BLACK);
		line.addView(shop_number);
		final TextView shop_thing=new TextView(ShoppingActivity.this);//操作
		shop_thing.setText("             操作");
		shop_thing.setTextSize(18);
		shop_thing.setGravity(Gravity.RIGHT);//居右对齐
		shop_thing.setTextColor(Color.BLACK);
		line.addView(shop_thing);
		shopinfo_content.addView(line);
		for(int i=0;i<foods.size();i++){
			
			LinearLayout line1=new LinearLayout(ShoppingActivity.this);
			line1.setClickable(true);
			line1.setOrientation(LinearLayout.HORIZONTAL);
			
			final TextView lunch_name=new TextView(ShoppingActivity.this);
			lunch_name.setText(foods.get(i).getName());
			lunch_name.setTextSize(18);
			lunch_name.setTextColor(Color.BLACK);
			line1.addView(lunch_name);
			
			
			final TextView lunch_price=new TextView(ShoppingActivity.this);
			//System.out.println("价格"+foods.get(i).getPrice());
			String text_price=new String();
			text_price=foods.get(i).getPrice()+"         ";
			lunch_price.setText(text_price);
			lunch_price.setTextSize(18);
			lunch_price.setTextColor(Color.BLACK);
			line1.addView(lunch_price);
			
			final TextView lunch_reduce=new TextView(ShoppingActivity.this);
			lunch_reduce.setId(i);
			lunch_reduce.setBackgroundResource(R.drawable.reduce);
			lunch_reduce.setHeight(18);
			lunch_reduce.setWidth(18);
			line1.addView(lunch_reduce);
			
			lunch_reduce.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int i=lunch_reduce.getId();//获取当前的组件id  用于辨识组件
					
						int number=foods.get(i).getCount()-1;
						foods.get(i).setCount(number);
						


					
				}
			});
			
			final TextView lunch_number=new TextView(ShoppingActivity.this);
			String text_number=new String();
			text_number="  "+foods.get(i).getCount()+"   ";
			lunch_number.setText(text_number);
			lunch_number.setTextSize(18);
			lunch_number.setTextColor(Color.BLACK);
			line1.addView(lunch_number);
			
			final TextView lunch_add=new TextView(ShoppingActivity.this);
			lunch_add.setId(20+i);
			lunch_add.setBackgroundResource(R.drawable.add);
			lunch_add.setHeight(18);
			lunch_add.setWidth(18);
			line1.addView(lunch_add);
			
			lunch_add.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int i=lunch_add.getId()-20;//获取当前的组件id  用于辨识组件
					int number=foods.get(i).getCount()+1;
					foods.get(i).setCount(number);
				}
			});
			
			final TextView lunch_delete=new TextView(ShoppingActivity.this);
			lunch_delete.setId(40+i);
			lunch_delete.setText("       删除");
			
			lunch_delete.setGravity(Gravity.RIGHT);//居右对齐
			lunch_delete.setTextSize(18);
			lunch_delete.setTextColor(Color.BLACK);
			line1.addView(lunch_delete);
			lunch_delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int i=lunch_delete.getId()-40;//获取当前的组件id  用于辨识组件
					foods.remove(i);
				}
			});
			shopinfo_content.addView(line1);
		}
		
		
		
		
	}
其中还可以对其进行监听,用不同的id进行标识,然后对其进行操作,其中的还可以刷新界面,将组件全部进行重构

相关推荐