sda*_*bet 24
假设您已经定义了自定义颜色属性,如下所示:
<declare-styleable name="color_view">
<attr name="my_color" format="color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
然后在视图的构造函数中,您可以检索如下颜色:
public ColorView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.color_view);
try {
int color = a.getColor(R.styleable.color_view_my_color, 0);
setBackgroundColor(color);
} finally {
a.recycle();
}
}
Run Code Online (Sandbox Code Playgroud)
你实际上不必担心如何填充颜色属性,就像这样
<com.test.ColorView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:my_color="#F00"
/>
Run Code Online (Sandbox Code Playgroud)
或者像这样:
<com.test.ColorView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:my_color="@color/red"
/>
Run Code Online (Sandbox Code Playgroud)
getColor
在任何情况下,该方法都将返回颜色值.
归档时间: |
|
查看次数: |
7436 次 |
最近记录: |