I'm trying to put a ShapeDrawable object inside a TableLayout cell. As suggested by the documentation here I create a custom View mCustomView
and then add the view to the TableLayout by doing开发者_StackOverflow社区
mRow.addView(mCustomView)
mTableLayout.addView(mRow)
Everything works fine, no errors but the shape is not displayed at all.
The code to create my drawable is
icon = new ShapeDrawable(new ArcShape(0, 360));
icon.setBounds(0, 0, 20, 20);
icon.getPaint().setColor(parseColor);
After some more digging and researching it turns out that the best solution is to pass the ShapeDrawable
object to an ImageView
constructor and then add this View to the table row with something like this
ShapeDrawable icon = new ShapeDrawable(new ArcShape(0, 360));
icon.getPaint().setColor(parseColor);
icon.setIntrinsicHeight(30);
icon.setIntrinsicWidth(30);
ImageView iv = new ImageView(this);
iv.setImageDrawable(icon);
mRow.addView(iv);
mTableLayout.addView(mRow)
This solution has also the advantage of getting rid of the custom View class.
精彩评论