我如何以<solid android:color= />编程方式更改?
我定义了一个自定义形状元素:my_item.xml:
<shape android:shape="oval">
<solid android:color="#FFFF0000"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
并在另一个布局中重用它:grid_view.xml:
<LinearLayout>
<ImageView ... android:src="@drawable/my_item"
android:id="@+id/myitemid" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
以下不起作用:
public class ShapeItemAdapter extends BaseAdapter {
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.grid_view, null);
ImageView shape = (ImageView) view.findViewById(R.id.myitemid);
shape.setBackgroundColor(0xBCCACA); //does not work
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这里提供的答案都不正确。重要的是:用于getDrawable()形状,而不是getBackground()。
GradientDrawable shape = (GradientDrawable) icon.getDrawable();
shape.setColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
尝试这个
ImageView shape = (ImageView) view.findViewById(R.id.myitemid);
GradientDrawable bgShape = (GradientDrawable)shape.getBackground();
bgShape.setColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10207 次 |
| 最近记录: |