首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式从styles.xml检索样式属性

如何以编程方式从styles.xml检索样式属性
EN

Stack Overflow用户
提问于 2012-12-05 16:34:41
回答 4查看 62K关注 0票数 90

目前,我正在使用WebView或TextView在我的应用程序中显示来自one服务的一些动态数据。如果数据包含纯文本,则使用TextView并应用styles.xml中的样式。如果数据包含超文本标记语言(主要是文本和图像),则使用WebView。

但是,此WebView是未设置样式的。因此,它看起来与通常的TextView有很大的不同。我读到过,只需在数据中直接插入一些WebView,就可以对文本设置样式。这听起来很简单,但是我想使用我的Styles.xml中的数据作为这个超文本标记语言中所需的值,这样如果我改变了我的样式,我就不需要在两个地方改变颜色等等。

那么,我如何才能做到这一点呢?我已经做了一些广泛的搜索,但我没有找到从您的styles.xml中实际检索不同样式属性的方法。我是不是遗漏了什么,或者真的不可能检索这些值?

我尝试从中获取数据的样式如下:

代码语言:javascript
复制
<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感兴趣。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-19 20:55:20

可以通过编程方式从styles.xml中检索自定义样式。

styles.xml中定义一些任意样式

代码语言:javascript
复制
<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>

现在,像这样检索样式

代码语言:javascript
复制
// 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()
票数 191
EN

Stack Overflow用户

发布于 2019-02-05 02:08:59

Ole和PrivatMamtora的答案对我来说不是很好,但这对我来说是有效的。

假设我想以编程方式读取此样式:

代码语言:javascript
复制
<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>

我可以这样做:

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2021-11-03 17:01:30

我不能让早期的解决方案起作用。

我的风格是:

代码语言:javascript
复制
<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的简单方法。

代码语言:javascript
复制
TextView sample = new TextView(new ContextThemeWrapper(getContext(), R.style.Widget_TextView_NumPadKey_Klondike), null, 0);

这给了我一个全样式的TextView来查询我想要的任何东西。例如:

代码语言:javascript
复制
float textSize = sample.getTextSize();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13719103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档