如何找回风格从styles.xml编程属性属性、风格、styles、xml

2023-09-12 06:23:02 作者:胭脂泪

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

Currently I'm using either a WebView or a TextView to show some dynamic data coming from a webservice in one of my apps. If the data contains pure text, it uses the TextView and applies a style from styles.xml. If the data contains HTML (mostly text and images) it uses the WebView.

不过,这是的WebView无样式。为此它看起来很多,从平时的TextView的不同。 我读过它可能只需直接插入一些HTML到数据的样式在web视图中的文本。这听起来很容易,但我想使用的数据从我Styles.xml在此HTML所需的值,所以我不需要改变颜色等等上的两个位置,如果我改变我的风格。

However, this WebView is unstyled. Therefor it looks a lot different from the usual TextView. I've read that it's possible to style the text in a WebView simply by inserting some HTML directly into the data. This sounds easy enough, but I would like to use the data from my Styles.xml as the values required in this HTML so I won't need to change the colors et cetera on two locations if I change my styles.

那么,我将如何能做到这一点?我做了一些广泛的搜索,但是我发现没有办法真正取回您的styles.xml不同的风格属性。我失去了一些东西在这里还是真的无法获取这些值?

So, how would I be able to do this? I've done some extensive searching but I have found no way of actually retrieving the different style attributes from your styles.xml. Am I missing something here or is it really not possible to retrieve these values?

我想从数据的样式如下:

The style I'm trying to get the data from is the following:

<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和文字颜色。

I'm mainly interested in the textSize and textColor.

在此先感谢。

推荐答案

这是可能的检索 styles.xml 自定义样式编程。

It is possible to retrieve custom styles from styles.xml programatically.

定义有些武断作风 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()