【问题标题】:How can i add more than 1 Views in one cell in TableRow?如何在 TableRow 的一个单元格中添加超过 1 个视图?
【发布时间】:2011-01-20 11:32:22
【问题描述】:

如上所述,我如何在 tablerow 的一个单元格中放置 2 个视图? 我创建了一个表格布局,并通过代码添加行。

下面是我的代码:

    TableLayout v = (TableLayout)inflater.inflate(R.layout.featureitem2, null);
//  v.setColumnStretchable(0, true);


    //adds each productname to the table
    if(productName.size()>1){
        TableRow pnamesRow = new TableRow(t);
        pnamesRow.addView(new View(t));
        for(int j=0;j < productName.size();j++){

            LinearLayout wrap = new LinearLayout(t);
            LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT);
            wrap.setLayoutParams(params);
            wrap.setOrientation(LinearLayout.HORIZONTAL);
            wrap.setVisibility(View.VISIBLE);

            ImageView remove = new ImageView(t);
            remove.setTag(j);
            remove.setClickable(true);
            remove.setImageDrawable(t.getResources().getDrawable(R.drawable.remove));   
            remove.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View arg0) {
                    int deletePosition = (Integer)arg0.getTag();
                    Log.v("pos","pos="+deletePosition);
                    removeChild(deletePosition);
                    notifyDataSetChanged();
                }

            });


            TextView pname = new TextView(t);
            pname.setText(productName.get(j));
            pname.setGravity(Gravity.CENTER);
        //  wrap.addView(pname);
            wrap.addView(remove);
            wrap.setLayoutParams(params);
            pnamesRow.addView(pname);
        //  pnamesRow.addView(pname);
        //  pnamesRow.addView(remove,j+1);
            //tbIndex++;
        }
        v.addView(pnamesRow);
    }

    for(int z =0;z < children.get(groupPosition).size();z++){
        TableRow tr2 =new TableRow(t); 
        Feature f = (Feature) children.get(groupPosition).get(z);
        TextView feature = new TextView(t);
        feature.setText(f.getFeaturename());
        feature.setTextColor(Color.BLACK);
        feature.setGravity(Gravity.CENTER);
        tr2.addView(feature);
        for(int k =0;k < f.getNumFeatures();k++){
            TextView value = new TextView(t);
            value.setText(f.getVal(k));
            value.setTextColor(Color.BLACK);
            //value.setGravity(Gravity.CENTER);
            value.offsetLeftAndRight(20);
            tr2.addView(value);
            v.setColumnShrinkable(k+1, true);
            tr2.setPadding(5, 5, 5, 5);
            tr2.offsetTopAndBottom(50);
        }

        v.addView(tr2);
    }

我想我已经找到了答案,感谢 commonsWare,

只有在将项目添加到linearlayout 之后,我才应该将layoutparams 设置为linearlayout

【问题讨论】:

    标签: android tableview tablerow


    【解决方案1】:

    您需要将它们放入某种容器中,例如LinearLayoutRelativeLayout。每个单元格只能有一个View,但如果那个View 是一个容器,那么那个View 实际上可以由几个组成。

    但请记住,手机屏幕很小。将容器嵌套在 TableLayout 中可能很难在例如 QVGA 屏幕上使用。

    【讨论】:

    • 嗨 ConmmonsWare,我试图将我的视图放在一个线性布局中,如上面的代码所示......但是当我运行应用程序时它不可见
    猜你喜欢
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多