gpt4 book ai didi

android - 以编程方式获取主题属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:28:20 25 4
gpt4 key购买 nike

如何以编程方式获取主题属性的值?

例如:

主题:

<style name="Theme">
... truncated ....
<item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
</style>

代码:

int textSize = ???? // how do I get Theme.textAppearanceLarge? 

编辑:没有解决问题的答案太多。

最佳答案

使用这个函数:

myView.setTextAppearance(Context context, int resid)
//Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

参见:http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance%28android.content.Context,%20int%29

在 TextView.java 中,上面的函数是这样做的:

public void setTextAppearance(Context context, int resid) {
TypedArray appearance =
context.obtainStyledAttributes(resid,
com.android.internal.R.styleable.TextAppearance);

int color;
ColorStateList colors;
int ts;

.
.
.
ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, 0);
if (ts != 0) {
setRawTextSize(ts);
}

int typefaceIndex, styleIndex;

typefaceIndex = appearance.getInt(com.android.internal.R.styleable.
TextAppearance_typeface, -1);
styleIndex = appearance.getInt(com.android.internal.R.styleable.
TextAppearance_textStyle, -1);

setTypefaceByIndex(typefaceIndex, styleIndex);
appearance.recycle();
}

这是另一种方法。请确保回收外观(TypedArray 对象)。希望这对您有所帮助!

关于android - 以编程方式获取主题属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8191529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com