目前,我正在使用WebView或TextView在我的应用程序中显示来自one服务的一些动态数据。如果数据包含纯文本,则使用TextView并应用styles.xml中的样式。如果数据包含超文本标记语言(主要是文本和图像),则使用WebView。
但是,此WebView是未设置样式的。因此,它看起来与通常的TextView有很大的不同。我读到过,只需在数据中直接插入一些WebView,就可以对文本设置样式。这听起来很简单,但是我想使用我的Styles.xml中的数据作为这个超文本标记语言中所需的值,这样如果我改变了我的样式,我就不需要在两个地方改变颜色等等。
那么,我如何才能做到这一点呢?我已经做了一些广泛的搜索,但我没有找到从您的styles.xml中实际检索不同样式属性的方法。我是不是遗漏了什么,或者真的不可能检索这些值?
我尝试从中获取数据的样式如下:
<style name="font4">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#E3691B</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:textStyle">bold</item>
</style>
我主要对textSize和textColor感兴趣。
发布于 2012-12-19 20:55:20
可以通过编程方式从styles.xml
中检索自定义样式。
在styles.xml
中定义一些任意样式
<style name="MyCustomStyle">
<item name="android:textColor">#efefef</item>
<item name="android:background">#ffffff</item>
<item name="android:text">This is my text</item>
</style>
现在,像这样检索样式
// The attributes you want retrieved
int[] attrs = {android.R.attr.textColor, android.R.attr.background, android.R.attr.text};
// Parse MyCustomStyle, using Context.obtainStyledAttributes()
TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs);
// Fetch the text from your style like this.
String text = ta.getString(2);
// Fetching the colors defined in your style
int textColor = ta.getColor(0, Color.BLACK);
int backgroundColor = ta.getColor(1, Color.BLACK);
// Do some logging to see if we have retrieved correct values
Log.i("Retrieved text:", text);
Log.i("Retrieved textColor as hex:", Integer.toHexString(textColor));
Log.i("Retrieved background as hex:", Integer.toHexString(backgroundColor));
// OH, and don't forget to recycle the TypedArray
ta.recycle()
发布于 2019-02-05 02:08:59
Ole和PrivatMamtora的答案对我来说不是很好,但这对我来说是有效的。
假设我想以编程方式读取此样式:
<style name="Footnote">
<item name="android:fontFamily">@font/some_font</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/black</item>
</style>
我可以这样做:
fun getTextColorSizeAndFontFromStyle(
context: Context,
textAppearanceResource: Int // Can be any style in styles.xml like R.style.Footnote
) {
val typedArray = context.obtainStyledAttributes(
textAppearanceResource,
R.styleable.TextAppearance // These are added to your project automatically.
)
val textColor = typedArray.getColorStateList(
R.styleable.TextAppearance_android_textColor
)
val textSize = typedArray.getDimensionPixelSize(
R.styleable.TextAppearance_android_textSize
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val typeface = typedArray.getFont(R.styleable.TextAppearance_android_fontFamily)
// Do something with the typeface...
} else {
val fontFamily = typedArray.getString(R.styleable.TextAppearance_fontFamily)
?: when (typedArray.getInt(R.styleable.TextAppearance_android_typeface, 0)) {
1 -> "sans"
2 -> "serif"
3 -> "monospace"
else -> null
}
// Do something with the fontFamily...
}
typedArray.recycle()
}
我从安卓的TextAppearanceSpan类中获得了一些灵感,你可以在这里找到它:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/TextAppearanceSpan.java
发布于 2021-11-03 17:01:30
我不能让早期的解决方案起作用。
我的风格是:
<style name="Widget.TextView.NumPadKey.Klondike" parent="Widget.TextView.NumPadKey">
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">?attr/wallpaperTextColorSecondary</item>
<item name="android:paddingBottom">0dp</item>
</style>
android.R.attr.textSize的obtainStyledAttributes()给出字符串结果"12sp“,然后我必须对其进行解析。对于android.R.attr.textColor,它提供了一个资源文件XML名称。这太麻烦了。
相反,我找到了一种使用ContextThemeWrapper的简单方法。
TextView sample = new TextView(new ContextThemeWrapper(getContext(), R.style.Widget_TextView_NumPadKey_Klondike), null, 0);
这给了我一个全样式的TextView来查询我想要的任何东西。例如:
float textSize = sample.getTextSize();
https://stackoverflow.com/questions/13719103
复制相似问题