我正在尝试创建一个彩色圆圈,并将其分配给我的GridView的ImageView。
下面是我的代码:
ImageView ivColor;
ivColor = new ImageView(MainActivity.this);
ivColor.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ShapeDrawable shape = new ShapeDrawable(new OvalShape());
shape.getPaint().setColor(color[i]);
shape.setIntrinsicWidth(128);
shape.setIntrinsicHeight(128);
ivColor.setImageDrawable(shape);现在,我手动将ShapeDrawable的高度和宽度设置为128px。有没有办法给ShapeDrawable赋值"match_parent“,就像我对ImageView一样?
发布于 2015-09-01 10:51:32
尝试更改行
ivColor.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 至
ivColor.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 编辑:
此外,为ImageView设置您必须运行setScaleType()
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
您可以根据需要尝试CENTER_CROP、CENTER_FILL或FIT_CENTRE。
发布于 2015-09-01 15:11:33
将ShapeDrawable设置为ImageView的背景。背景总是充满了View,所以你会得到你想要实现的东西。
ivColor.setBackgroundDrawable(shape);或
ivColor.setBackground(shape); // Api level 16+https://stackoverflow.com/questions/32318331
复制相似问题