动态创建的TableRow,然后RelativeLayout的不显示 [英] Dynamically created TableRow and then RelativeLayout not showing

查看:229
本文介绍了动态创建的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();
            }        }


解决方案

  

如果我尝试设置为RelativeLayout的布局PARAMS然后什么
  出现并没有错误。如果我不这么做,一切都显得但
  布局被设置为wrapcontent


您使用了错误的的LayoutParams RelativeLayout的。您可以使用:

  RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
row.addView(RelativeLayout的,RLP);

但作为父 RelativeLayout的的TableRow 您必须使用的TableRow。的LayoutParams

  TableRow.LayoutParams RLP =新TableRow.LayoutParams(
                        TableRow.LayoutParams.FILL_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT);
row.addView(RelativeLayout的,RLP);

I am iterating through the results of a query creating RelativeLayouts in TableRows. If I try to set the LayoutParams for the RelativeLayout then nothing appears and get no errors. If I don't then everything appears but with the layout being set to WRAP_CONTENT. I have tried several example and can not get it to work. Below is my code:

            TableLayout tblItems = (TableLayout) findViewById(R.id.tblItems);

            // Fields from the database (projection)
            String[] projection = new String[] { ItemsTable.ITEM_ID,
                                                ItemsTable.ITEM_NAME,
                                                ItemsTable.ITEM_PRICE,
                                                ItemsTable.ITEM_PICTURE };          
            Cursor cursor = getContentResolver().query(ItemsTable.CONTENT_URI, projection, null, null, null);                   
            startManagingCursor(cursor);        

            // Establish the item mapping
//          String[] columns = new String[] {"name", "price"};
//          int[] to = new int[] {R.id.tv_description, R.id.tv_price};

//          adapter = new SimpleCursorAdapter(this, R.layout.item_list_select, cursor, columns, to, 0);
//          lvSelectItems.setAdapter(adapter);

/*          
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 

                public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
                    if(columnIndex == cursor.getColumnIndexOrThrow(OrderDetailTable.ORDERDETAIL_PRICE)) { 

                  //          String createDate = aCursor.getString(aColumnIndex); 
                  //          TextView textView = (TextView) aView; 
                  //          textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate)); 

                            double total = cursor.getDouble(columnIndex);
                            TextView textView = (TextView) view;
                            textView.setText("$" + String.format("%.2f",dRound(total, 2)));

                            return true; 
                    } 
                    return false; 
                } 

            });
*/          
            // tblItems

            int totalRows = 0;
            int tcolumn = 1;

            int numItems = cursor.getCount();
            double tempRow = numItems / 2;

            if (numItems > 0) {
                itemsFound = true;
            } else {
                itemsFound = false;
            }

            long iPart;  // Integer part
            double fPart;  // Fractional part
            boolean ifFirst = true;

            // Get user input
            iPart = (long) tempRow;
            fPart = tempRow - iPart;

            if (fPart > 0) {
                totalRows = (int) (iPart + 1);
            } else {
                totalRows = (int) iPart;
            }

            TableRow row = null;

            cursor.moveToFirst(); 
            while (cursor.isAfterLast() == false)  
            { 
                // tblItems    // TableLayout
                // TableRow
                // RelativeLayout
                // ImageView
                // TextView
                // TextView

                if (ifFirst) {
                    // create a new TableRow
                    row = new TableRow(this);
                    ifFirst = false;
                }

                // Creating a new RelativeLayout
                RelativeLayout relativeLayout = new RelativeLayout(this);


                // Defining the RelativeLayout layout parameters.
                // In this case I want to fill its parent
                RelativeLayout.LayoutParams rlp = new 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);

                // add text view
                ImageView imgPic = new ImageView(this);
                imgPic.setBackgroundResource(R.drawable.takepic);
                imgPic.setPadding(0, 0, 0, 0);
                relativeLayout.addView(imgPic);

                // add text view
                TextView tvName = new TextView(this);
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_NAME));
                tvName.setText("" + name);

             // Defining the layout parameters of the TextView
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);

           //     RelativeLayout.

                // Setting the parameters on the TextView
                tvName.setLayoutParams(lp);
                relativeLayout.addView(tvName);

                // add text view
                double iPrice = cursor.getDouble(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_PRICE));

                TextView tvPrice = new TextView(this);
                tvPrice.setText("$" + String.format("%.2f",dRound(iPrice, 2)));
                relativeLayout.addView(tvPrice);


                tcolumn++;

                numItems--;

                if (tcolumn == 3) {
                    // add the TableRow to the TableLayout
                    tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                    // create a new TableRow
                    row = new TableRow(this);
                    tcolumn = 1;
                } else {
                    if (numItems == 0) {
                        // add the TableRow to the TableLayout
                        tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                }

                // Move to the next record
                cursor.moveToNext(); 
            } 

        }

解决方案

If I try to set the layout params for the relativeLayout then nothing appears and get no errors. If I don't then everything appears but with the layout being set to wrapcontent.

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的不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆