如何在 TableRow 的一个单元格中添加超过 1 个视图?

发布于 2024-08-22 22:56:44 字数 2805 浏览 5 评论 0原文

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

下面是我的代码:

    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,

只有在我将项目添加到 中之后,我才应该将 layoutparams 设置为 线性布局 >线性布局

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.

Below is my code,:

    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);
    }

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-08-29 22:56:44

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文