【问题标题】:How to give border of cells in tablelayout in android如何在android中的tablelayout中给出单元格的边框
【发布时间】:2016-09-02 08:03:15
【问题描述】:

我有一个问题,如何以编程方式在 Android 的表格布局中赋予单元格边框。

这是我的代码:

 TableLayout table = (TableLayout)findViewById(R.id.linear_Layout_List);
         TableRow row=null;
         TableRow row1=null;
         Button label = null;

         int size=items.size();
         for (int i = 0; i < size-2; i+=3)
         {
         row = new TableRow(SearchGridActivity.this);
         row.setId(100+i);

         for(j=0;j<3;j++)
         {
            /*TableLayout.LayoutParams tableRowParams=
                  new TableLayout.LayoutParams
                  (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
            int leftMargin=10;
            int topMargin=2;
            int rightMargin=10;
            int bottomMargin=2;

            tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

            row.setLayoutParams(tableRowParams);*/
            if(count_img!=NUM_RESULTS)
            {
            //row.setPadding(10, 2, 10, 0);

            View v = View.inflate(this, R.layout.tablecell, null);
            LinearLayout lay = (LinearLayout)v.findViewById(R.id.linear_table);
            /*MarginLayoutParams layoutParams = new MarginLayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

                layoutParams.setMargins(30, 20, 30, 0);*/

            final ImageView img = new ImageView(this);

            img.setImageBitmap(imageBitmap[count_img]);
            img.setClickable(true);
            img.setId(count_img);

            img.setOnClickListener(new View.OnClickListener() {


                    public void onClick(View arg0) {

                         BaseInventoryItem item = items.get(img.getId());
                            if (item instanceof Offer) {

                                SearchResultActivity.list_flag = false;
                                grid_flag = true;

                                Intent intent = new Intent(SearchGridActivity.this, ProductComparisonActivity.class);
                                intent.putExtra("item", item);
                                startActivity(intent);

                            } else if (item instanceof Product) {

                                SearchResultActivity.list_flag = false;
                                grid_flag = true;

                                Intent intent = new Intent(SearchGridActivity.this, ProductComparisonActivity.class);
                                intent.putExtra("item", item);
                                startActivity(intent);
                            }
                    }

                });
             BaseInventoryItem item = items.get(count_img);

             TextView txt_price = new TextView(this);

              if (item instanceof Offer) {
                  Offer offer = (Offer) item;
                  txt_price.setText(offer.getPrice().getPrice());
                  txt_price.setTextColor(Color.BLACK);
                  txt_price.setTextSize(15);
              } else if (item instanceof Product) {
                  Product product = (Product) item;
                  txt_price.setText("from " + product.getPriceSet().getMinPrice().getPrice());
                  txt_price.setTextColor(Color.BLACK);
                  txt_price.setTextSize(15);
              }



            lay.addView(img);
            lay.addView(txt_price);
            row.addView(v);
            count_img++;
            }
         }
         //row.addView(label);

         table.addView(row);


         }
        /* row1 = new TableRow(SearchGridActivity.this);
         row1.setId(10001);*/
         TableRow row2 = (TableRow) View.inflate(this, R.layout.tablerow, null);
         Button btn_load = (Button)row2.findViewById(R.id.btn_table_LoadMore);
         btn_load.setOnClickListener(new View.OnClickListener() {


                public void onClick(View arg0) {

                    NUM_RESULTS = NUM_RESULTS+12;
                    Intent intent = new Intent(SearchGridActivity.this,SearchGridActivity.class);
                    intent.putExtra("keyword", keyword);
                    startActivity(intent);
                }
            });
        /* row1.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT,
         LayoutParams.FILL_PARENT));*/
         TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
                 TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT);
         rowSpanLayout.span = 3;


         //row.setBackgroundColor(0xff222222);

        label = new Button(SearchGridActivity.this);
        label.setText("Load More");


        //label.setBackgroundColor(Color.BLUE);


         //row1.addView(label);

         //row.addView(label);

         table.addView(row2,rowSpanLayout);

【问题讨论】:

    标签: android


    【解决方案1】:

    没有直接的方法可以做到这一点。您必须弄乱 backgroundma​​rginspadding

    我最终使用了类似的东西

    <TableLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*" android:background="#ff0000">
    <TableRow android:background="#00ff00" android:layout_margin="2dip">
        <Button android:id="@+id/button" android:text="+" android:background="#0000ff" android:layout_margin="2dip"/>
        <TextView android:text="@string/label"  android:background="#0000ff" android:layout_margin="2dip"/>
        <TextView android:id="@+id/amount"  android:background="#0000ff" android:layout_margin="2dip"/>
    </TableRow>
    

    取自这里。 LINK

    【讨论】:

    • 但是山姆我正在使用非硬编码的代码执行此操作,请建议我针对非硬编码格式的代码的解决方案。感谢您的回复
    • 答案是一样的。你必须弄乱背景、边距和填充。在代码或 xml 中。
    【解决方案2】:

    另外两个解决方案有效,但并不优雅。 我的解决方案是在“可绘制”新文件中创建,例如cell_shape.xml,内容:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape= "rectangle"  >
            <solid android:color="#0fff"/>
            <stroke android:width="1dp"  android:color="#000"/>
    </shape>
    

    在我的代码中,我有创建新单元格的方法:

    private TextView initPlainTextView() {
        TextView textView = new TextView(getContext());
        textView.setPadding(CELL_PADDING, CELL_PADDING, CELL_PADDING, CELL_PADDING);
        textView.setBackgroundResource(R.drawable.cell_shape);
        return textView;
    }
    

    在其他方法中,我会执行以下操作:

    TableRow rowHeader = new TableRow(getContext());
    rowHeader.addView(initPlainTextView("cell0"));
    rowHeader.addView(initPlainTextView("cell1"));
    tableLayout.addView(rowHeader);
    

    PS:灵感来自https://stackoverflow.com/a/7379990/1219241

    【讨论】:

      【解决方案3】:

      布局参数也必须以编程方式设置。

      您可以看到下面的示例代码,这在动态创建行后对我有用。

      TableRow row = new TableRow(this);
      
      TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
          TableLayout.LayoutParams.WrapContent,   
          TableLayout.LayoutParams.WrapContent
      );
      
      tableRowParams.SetMargins(3, 3, 2, 10);
      
      row.LayoutParameters = tableRowParams;
      

      这在当前的 Android SDK 上失败,因为 row.LayoutParameters 不可见。 将其更改为:

      row.setLayoutParams(tableRowParams);
      

      【讨论】:

        猜你喜欢
        • 2013-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 2013-06-14
        相关资源
        最近更新 更多