Domanda

Come sopra, come faccio a mettere 2 viste all'interno di una cella in una tablerow? Ho creato una tabella di layout e per codice sto aggiungendo righe.

Di seguito è il mio codice ,:

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

Credo di aver trovato la risposta, grazie a commonsWare,

devo impostare il layoutparams al linearlayout solo dopo aggiungo gli oggetti nel linearlayout.

È stato utile?

Soluzione

Si avrebbe bisogno di metterli in una sorta di contenitore, come ad esempio un LinearLayout o RelativeLayout. Ci può essere un solo View per cella, ma se questo View è un contenitore, poi quello View può essere in realtà costituito da vari.

Tenete a mente, però, che le schermate del telefono sono molto piccoli. Avendo contenitori annidati all'interno di un TableLayout può essere difficile da usare su, ad esempio, uno schermo QVGA.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top