Question

I wrote a function that injects a row in my TableLayout. I've got my TableLayout defined as followed:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mytable"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</TableLayout>

And here it the function:

public void injectRow(String text, String result){
        TableRow tableRow = new TableRow(this);
        tableRow.setBackgroundColor(0xFF0000);
        TableRow.LayoutParams tableParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        tableRow.setLayoutParams(tableParams);

        Resources r = getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());

        TextView t1 = new TextView(this);
        TableRow.LayoutParams t1params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, Math.round(px), 0.65f);
        t1.setText(text);
        t1.setTextSize(20);
        t1.setTextColor(0xFFF910);
        t1.setGravity(Gravity.CENTER_VERTICAL);
        t1.setLayoutParams(t1params);

        tableRow.addView(t1);

        TextView t2 = new TextView(this);
        TableRow.LayoutParams t2params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, Math.round(px), 0.35f);
        t2.setText(result);
        t2.setTextSize(20);
        t2.setTextColor(0xFFF910);
        t2.setGravity(Gravity.CENTER);
        t2.setLayoutParams(t2params);

        tableRow.addView(t2);

        table.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
    }

I've tried different layout params aswell as adding the orientation and a background color (red) to the row to see if the textviews were just invisible, but nope. I've also given the textviews a fixed height, but to no use..

So what is going wrong? A lot of treads on SO say to pass along TableLayout params in the table.addview, but that also doesn't work for me...

Était-ce utile?

La solution

You are using transparent colors everywhere, both setColor and setBackground expect an ARGB value, so colors would be: 0xFFFFF910 , and 0xFFFF0000

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top