如何在TableRow的一个单元格中添加超过1个视图? [英] How can i add more than 1 Views in one cell in TableRow?

查看:71
本文介绍了如何在TableRow的一个单元格中添加超过1个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As above, How do i put 2 views inside one cell in a tablerow? I created a Table Layout and by code i am adding rows.

下面是我的代码:

    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,


I think i have found the answer, thanks to commonsWare,

仅在将项目添加到 linearlayout 中之后,才应将 layoutparams 设置为 linearlayout .

I should set the layoutparams to the linearlayout only after i add the items into the linearlayout.

推荐答案

您需要将它们放入某种容器中,例如 LinearLayout RelativeLayout .每个单元格只能有一个 View ,但是如果该 View 是一个容器,则该 View 实际上可以由多个容器组成.

You would need to put them in some sort of container, such as a LinearLayout or RelativeLayout. There can only be one View per cell, but if that View is a container, then that one View can be actually made up of several.

请记住,电话屏幕很小.将容器嵌套在 TableLayout 内可能很难在QVGA屏幕上使用.

Bear in mind, though, that phone screens are tiny. Having containers nested inside a TableLayout may be difficult to use on, say, a QVGA screen.

这篇关于如何在TableRow的一个单元格中添加超过1个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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