【问题标题】:Not able to add view dynamically无法动态添加视图
【发布时间】:2015-02-15 12:31:49
【问题描述】:

我在 XML 中有线性布局 R.id.linearLayoutQuantityPrice。

我想在其中动态添加一个新的线性布局。

新的线性布局有两个文本视图和一个图像视图。

List<Price> priceList = database.getItemQuantityPrice(id);
        LinearLayout linearLayoutQuantityPrice = (LinearLayout) rowView
                .findViewById(R.id.linearLayoutQuantityPrice);
        for (int i=0; i<priceList.size() ; i++) {
            Price price = new Price();
            price = priceList.get(i);
            LinearLayout newLinearLayout = new LinearLayout(context);
            newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            LayoutParams newLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            newLinearLayout.setPadding(2, 2, 2, 2);
            newLinearLayout.setWeightSum(3);

            int iqpId = price.getIqpId();
            String quantityIn = price.getQuantityIn();
            int quantity = price.getItmQnt();
            int itemPrice = price.getItmPrc();

            TextView tvQuantity=new TextView(context);
            tvQuantity.setId(price.getIqpId()+10);
            tvQuantity.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
            tvQuantity.setTextSize(12);
            if(quantityIn.equalsIgnoreCase("gm") && quantity>=1000){
                quantity = quantity/1000;
                quantityIn = new String("KG");
            }
            tvQuantity.setText(""+quantity+" "+quantityIn);
            newLinearLayout.addView(tvQuantity);

            TextView tvPrice=new TextView(context);
            tvPrice.setId(price.getIqpId()+11);
            tvPrice.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
            tvPrice.setTextSize(12);
            tvPrice.setText("Rs. "+itemPrice);
            newLinearLayout.addView(tvPrice);

            ImageView ivButton = new ImageView(context);
            ivButton.setId(iqpId);
            ivButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f));
            ivButton.setImageDrawable(rowView.getResources().getDrawable(R.drawable.add_button));
            ivButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int toCartId = v.getId();
                    Toast.makeText(context, "Added to cart" + toCartId, Toast.LENGTH_SHORT).show();
                }
            });
            linearLayoutQuantityPrice.addView(newLinearLayout);
        }

代码没有出现任何错误。但未添加所需的视图。

【问题讨论】:

  • 也可以拨打newLinearLayout.setLayoutParams(newLayoutParams)

标签: android android-layout android-linearlayout


【解决方案1】:

因为你忘了设置布局参数。

newLinearLayout.setLayoutParams(newLayoutParams)

【讨论】:

    【解决方案2】:

    尝试使用它。这对我有用。

    ArrayList<Component> components;
    
            LinearLayout llRowContainer;
    
            llRowContainer = (LinearLayout) findViewById(R.id.ll_row_container);
    
    
            components = IappApp.getContext().getDBManager()
                    .getComponentList(COMPONENT_TYPE, ACTIVITY_TYPE);
    
    
            int index = 0;
            for (Component component : components) {
    
                View rowView = View.inflate(this, R.layout.dae_general_info_row , null);
    
                llRowContainer.addView(rowView);
    
                final TextView txtViewTitle = (TextView) rowView
                        .findViewById(R.id.txtViewTitle);
    
                final EditText editTextAns = (EditText) rowView
                        .findViewById(R.id.editTextAns);
    
                editTextAns.setHint(component.getUnit()+"");
    
                txtViewTitle.setText(++index + ". " + component.getTitle());
                editTextAns.setTag(component);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2014-09-10
      • 1970-01-01
      相关资源
      最近更新 更多