本文介绍了动态创建的TableRow,然后RelativeLayout的不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过查询生成 RelativeLayouts 的结果 TableRows 迭代。如果我尝试设置了的LayoutParams RelativeLayout的则没有出现,并得到没有错误。如果我不这么做,一切都显得但与布局被设置为 WRAP_CONTENT 。我曾尝试几个例子并不能得到它的工作。下面是我的code:

  TableLayout tblItems =(TableLayout)findViewById(R.id.tblItems);            //字段从数据库(凸起)
            的String [] =投影新的String [] {ItemsTable.ITEM_ID,
                                                ItemsTable.ITEM_NAME,
                                                ItemsTable.ITEM_PRICE,
                                                ItemsTable.ITEM_PICTURE};
            光标光标= getContentResolver()查询(ItemsTable.CONTENT_URI,投影,NULL,NULL,NULL);
            startManagingCursor(光标);            //建立项目映射
//的String [] =列新的String [] {姓名,价格};
// INT []为= INT新[] {R.id.tv_description,R.id.tv_price};//适配器=新SimpleCursorAdapter(这一点,R.layout.item_list_select,光标,列,至,0);
// lvSelectItems.setAdapter(适配器);/ *
            adapter.setViewBinder(新SimpleCursorAdapter.ViewBinder(){                公共布尔setViewValue(查看视图,光标光标,诠释参数:columnIndex){
                    如果(参数:columnIndex == cursor.getColumnIndexOrThrow(OrderDetailTable.ORDERDETAIL_PRICE)){                  //字符串CREATEDATE = aCursor.getString(aColumnIndex);
                  // TextView中的TextView =(TextView中)aView;
                  // textView.setText(创建日期:+ MyFormatterHelper.formatDate(getApplicationContext(),CREATEDATE));                            双总= cursor.getDouble(参数:columnIndex);
                            TextView中的TextView =(TextView的)视图。
                            textView.setText($+的String.format(。%2F,dRound(总的,2)));                            返回true;
                    }
                    返回false;
                }            });
* /
            // tblItems            INT totalRows = 0;
            INT tcolumn = 1;            INT numItems的= cursor.getCount();
            双tempRow = numItems的/ 2;            如果(numItems的大于0){
                itemsFound =真;
            }其他{
                itemsFound = FALSE;
            }            长的iPart; //整数部分
            双fPart; //小数部分
            布尔ifFirst =真;            //获取用户输入
            的iPart =(长)tempRow;
            fPart = tempRow - 的iPart;            如果(fPart大于0){
                totalRows =(int)的(的iPart + 1);
            }其他{
                totalRows =(INT)的iPart;
            }            连续的TableRow = NULL;            cursor.moveToFirst();
            而(cursor.isAfterLast()== FALSE)
            {
                // // tblItems TableLayout
                //的TableRow
                //的RelativeLayout
                // ImageView的
                //的TextView
                //的TextView                如果(ifFirst){
                    //创建一个新的TableRow
                    行=新的TableRow(本);
                    ifFirst = FALSE;
                }                //创建一个新的RelativeLayout
                的RelativeLayout的RelativeLayout =新的RelativeLayout(本);
                //定义RelativeLayout的布局参数。
                //在这种情况下,我想,以填补其母公司
                RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);                rlp.setMargins(5,5,5,5);                relativeLayout.setPadding(5,5,5,5);
                relativeLayout.setBackgroundResource(R.drawable.grpbx_item);                row.addView(RelativeLayout的,RLP);                //添加文本视图
                ImageView的imgPic =新ImageView的(本);
                imgPic.setBackgroundResource(R.drawable.takepic);
                imgPic.setPadding(0,0,0,0);
                relativeLayout.addView(imgPic);                //添加文本视图
                TextView的tvName =新的TextView(本);
                字符串名称= cursor.getString(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_NAME));
                tvName.setText(+名称);             //定义的TextView的布局参数
                RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);           // RelativeLayout的。                //在TextView的设置参数
                tvName.setLayoutParams(LP);
                relativeLayout.addView(tvName);                //添加文本视图
                双iPrice = cursor.getDouble(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_PRICE));                TextView的tvPrice =新的TextView(本);
                tvPrice.setText($+的String.format(%2f上。,dRound(iPrice,2)));
                relativeLayout.addView(tvPrice);
                tcolumn ++;                numItems--;                如果(tcolumn == 3){
                    //将的TableRow添加到TableLayout
                    tblItems.addView(行,新TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));                    //创建一个新的TableRow
                    行=新的TableRow(本);
                    tcolumn = 1;
                }其他{
                    如果(numItems的== 0){
                        //将的TableRow添加到TableLayout
                        tblItems.addView(行,新TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    }
                }                //移动到下一个记录
                cursor.moveToNext();
            }        }


解决方案

You use the wrong LayoutParams for the RelativeLayout. You use:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);   
row.addView(relativeLayout, rlp);

but as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams:

TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                        TableRow.LayoutParams.FILL_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT);   
row.addView(relativeLayout, rlp);

这篇关于动态创建的TableRow,然后RelativeLayout的不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 10:19