简体   繁体   中英

Android Table Cells Border

This xml code below gives me a proper border around my table cells

<TableRow android:background="#cdcdcd" > 
   <TextView android:text="1st Column" android:background="#ffffff" android:layout_margin="2dip"/>
   <TextView android:text="2nd Column" android:background="#ffffff" android:layout_margin="2dip"/>
   <TextView android:id="@+id/amount"  android:background="#ffffff"  android:layout_margin="2dip" android:text="3rd col"/>
</TableRow>

But when i try to do it programmatically it is doesn't work here is my code:

    dataTable = (TableLayout)findViewById(R.id.data_table);             
    TableRow tr=new TableRow(this); 
    counter++;
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    lp.setMargins(2,2, 2, 2);

    TextView tv= new TextView(this);
    tv.setLayoutParams(lp);
    tv.setText("text"+counter);
    tv.setBackgroundColor(Color.parseColor("#ffffff"));             
    counter++;

    TextView cb= new TextView(this);
    cb.setLayoutParams(lp);
    cb.setText("text"+counter);
    counter++;

    TextView ib= new TextView(this);
    ib.setText("text"+counter);
    ib.setLayoutParams(lp);     

    tr.setBackgroundColor(Color.parseColor("#cdcdcd"));
    tr.setPadding(2, 2, 2, 2);
    tr.addView(tv);
    tr.addView(cb);
    tr.addView(ib);
    dataTable.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

Every thing works except for the borders and when i set "tv.setLayoutParams(lp);" the parameters the column disappears.

Try to add the view by calling addView(View child, int index, ViewGroup.LayoutParams params)

Also, try to create a new LayoutParams instance for each view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM