如何检索风格从styles.xml编程属性

问题描述 投票:69回答:4

目前我使用一个或网页视图一个TextView来显示一些动态数据从一个Web服务来在我的应用程序之一。如果数据包含纯文本,它使用的TextView和styles.xml适用的样式。如果数据包含HTML(主要是文字和图片),它使用的WebView。

然而,这Webview是无样式。为此它看起来与平常的TextView有很大不同。我读过,它可以简单地通过直接插入一些HTML到数据样式中的WebView文本。这听起来很容易,但我想从我的Styles.xml使用数据在本HTML所需的价值观,所以我不会需要改变颜色上的两个位置等等,如果我改变我的风格。

所以,我怎么能做到这一点?我做了一些广泛的搜索,但我发现没有真正从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和文字颜色。

android xml styles
4个回答
156
投票

它可以从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()

43
投票

@Ole给答案似乎使用某些属性,如则shadowColor,shadowDx,shadowDy,shadowRadius(这些只是少数,我发现,有可能是更多)时,打破

我不知道,为什么会出现此问题,这我问here,但@AntoineMarques编码风格似乎解决了问题。

为了与任何属性这项工作会是这样的


First, define a stylable to contain the resource ids like so

attrs.xml

<resources>     
    <declare-styleable name="MyStyle" >
        <attr name="android:textColor" />
        <attr name="android:background" />
        <attr name="android:text" />
    </declare-styleable>
</resources>

然后,在代码中你会做这样得到的文本。

TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, R.styleable.MyStyle);  
String text = ta.getString(R.styleable.MyStyle_android_text);

使用这种方法的好处是,你检索由名称值,而不是一个指标。


0
投票

从奥莱和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()
}

我从Android的TextAppearanceSpan类的一些灵感,你可以在这里找到:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/TextAppearanceSpan.java


-2
投票

如果接受的解决方案不是尝试合作,attr.xml重命名为attrs.xml(为我工作)

© www.soinside.com 2019 - 2024. All rights reserved.