如何以编程方式获取 ?attr/ 值
How to get an ?attr/ value programmatically
我正在尝试进行一些自定义视图样式设置,但在从主题中正确选取样式属性时遇到了问题。
例如,我想获取主题 EditText 的文本颜色。
查看主题堆栈,您可以看到我的主题使用它来设置 EditText 的样式:
<style name="Base.V7.Widget.AppCompat.EditText" parent="android:Widget.EditText">
<item name="android:background">?attr/editTextBackground</item>
<item name="android:textColor">?attr/editTextColor</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
我正在寻找的是如何获得它?attr/editTextColor
(又名,主题赋予的价值"android:editTextColor")
通过 google 搜索,我找到了足够多的答案:
TypedArray a = mView.getContext().getTheme().obtainStyledAttributes(R.style.editTextStyle, new int[] {R.attr.editTextColor});
int color = a.getResourceId(0, 0);
a.recycle();
但我很确定我一定做错了,因为它总是显示为黑色而不是灰色?
你试过吗?
编辑:
这是我的简短回答,如果你想要一个完整的答案,请问我
您的 attrs.xml 文件:
<resources>
<declare-styleable name="yourAttrs">
<attr name="yourBestColor" format="color"/>
</declare-styleable>
</resources>
编辑 2 : 抱歉我忘了展示我是如何在 layout.xml 中使用我的 attr 值的,所以 :
<com.custom.coolEditext
android:id="@+id/superEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:yourBestColor="@color/any_color"/>
然后在您的自定义编辑文本中:
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.yourAttrs, 0, 0);
try {
int colorResource = a.getColor(R.styleable.yourAttrs_yourBestColor, /*default color*/ 0);
} finally {
a.recycle();
}
我不确定这是否是你想要的答案,但它可以让你走上正轨
@pskink 的评论:
TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.editTextColor, value, true);
getView().setBackgroundColor(value.data);
将从当前分配给上下文的主题中提取属性。
谢谢@pskink!
我正在尝试进行一些自定义视图样式设置,但在从主题中正确选取样式属性时遇到了问题。
例如,我想获取主题 EditText 的文本颜色。
查看主题堆栈,您可以看到我的主题使用它来设置 EditText 的样式:
<style name="Base.V7.Widget.AppCompat.EditText" parent="android:Widget.EditText">
<item name="android:background">?attr/editTextBackground</item>
<item name="android:textColor">?attr/editTextColor</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
我正在寻找的是如何获得它?attr/editTextColor
(又名,主题赋予的价值"android:editTextColor")
通过 google 搜索,我找到了足够多的答案:
TypedArray a = mView.getContext().getTheme().obtainStyledAttributes(R.style.editTextStyle, new int[] {R.attr.editTextColor});
int color = a.getResourceId(0, 0);
a.recycle();
但我很确定我一定做错了,因为它总是显示为黑色而不是灰色?
你试过吗?
编辑: 这是我的简短回答,如果你想要一个完整的答案,请问我
您的 attrs.xml 文件:
<resources>
<declare-styleable name="yourAttrs">
<attr name="yourBestColor" format="color"/>
</declare-styleable>
</resources>
编辑 2 : 抱歉我忘了展示我是如何在 layout.xml 中使用我的 attr 值的,所以 :
<com.custom.coolEditext
android:id="@+id/superEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:yourBestColor="@color/any_color"/>
然后在您的自定义编辑文本中:
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.yourAttrs, 0, 0);
try {
int colorResource = a.getColor(R.styleable.yourAttrs_yourBestColor, /*default color*/ 0);
} finally {
a.recycle();
}
我不确定这是否是你想要的答案,但它可以让你走上正轨
@pskink 的评论:
TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.editTextColor, value, true);
getView().setBackgroundColor(value.data);
将从当前分配给上下文的主题中提取属性。
谢谢@pskink!