QMUI_Android
QMUI_Android copied to clipboard
建议:修改QMUIResHelper实现,以解决AS预览xml布局时不生效的问题
object QMUIResHelper {
@JvmStatic
fun getAttrFloatValue(context: Context, attr: Int): Float {
return getAttrFloatValue(context.theme, attr)
}
@JvmStatic
fun getAttrFloatValue(theme: Resources.Theme, attr: Int): Float {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attr
val a: TypedArray = theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getFloat(0, 0f)
a.recycle()
return v
}
@JvmStatic
fun getAttrColor(context: Context, attrRes: Int): Int {
return getAttrColor(context.theme, attrRes)
}
@JvmStatic
fun getAttrColor(theme: Resources.Theme, attr: Int): Int {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attr
val a: TypedArray = theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getColor(0, Color.BLACK)
a.recycle()
return v
}
@JvmStatic
fun getAttrColorStateList(context: Context, attrRes: Int): ColorStateList? {
return getAttrColorStateList(context, context.theme, attrRes)
}
@JvmStatic
fun getAttrColorStateList(
context: Context?,
theme: Resources.Theme,
attr: Int
): ColorStateList? {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attr
val a: TypedArray = theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getColorStateList(0)
a.recycle()
return v
}
@JvmStatic
fun getAttrDrawable(context: Context, attr: Int): Drawable? {
return getAttrDrawable(context, context.theme, attr)
}
@JvmStatic
fun getAttrDrawable(context: Context, theme: Resources.Theme, attr: Int): Drawable? {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attr
val a: TypedArray = theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getDrawable(0)
a.recycle()
return v
}
@JvmStatic
fun getAttrDrawable(context: Context, typedArray: TypedArray, index: Int): Drawable? {
val v = typedArray.getDrawable(index)
typedArray.recycle()
return v
}
@JvmStatic
fun getAttrDimen(context: Context, attrRes: Int): Int {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attrRes
val a: TypedArray = context.theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getDimensionPixelOffset(0, 0)
a.recycle()
return v
}
@JvmStatic
fun getAttrString(context: Context, attrRes: Int): String? {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attrRes
val a: TypedArray = context.theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getString(0)
a.recycle()
return v
}
@JvmStatic
fun getAttrInt(context: Context, attrRes: Int): Int {
val sAttrResArray = IntArray(1)
sAttrResArray[0] = attrRes
val a: TypedArray = context.theme.obtainStyledAttributes(0, sAttrResArray)
val v = a.getInt(0, 0)
a.recycle()
return v
}
@JvmStatic
fun assignTextViewWithAttr(textView: TextView, attrRes: Int) {
val a = textView.context.obtainStyledAttributes(null, R.styleable.QMUITextCommonStyleDef, attrRes, 0)
val count = a.indexCount
var paddingLeft = textView.paddingLeft
var paddingRight = textView.paddingRight
var paddingTop = textView.paddingTop
var paddingBottom = textView.paddingBottom
for (i in 0 until count) {
val attr = a.getIndex(i)
if (attr == R.styleable.QMUITextCommonStyleDef_android_gravity) {
textView.gravity = a.getInt(attr, -1)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_textColor) {
textView.setTextColor(a.getColorStateList(attr))
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_textSize) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, a.getDimensionPixelSize(attr, 0).toFloat())
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_paddingLeft) {
paddingLeft = a.getDimensionPixelSize(attr, 0)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_paddingRight) {
paddingRight = a.getDimensionPixelSize(attr, 0)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_paddingTop) {
paddingTop = a.getDimensionPixelSize(attr, 0)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_paddingBottom) {
paddingBottom = a.getDimensionPixelSize(attr, 0)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_singleLine) {
textView.isSingleLine = a.getBoolean(attr, false)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_ellipsize) {
val ellipsize = a.getInt(attr, 3)
when (ellipsize) {
1 -> textView.ellipsize = TextUtils.TruncateAt.START
2 -> textView.ellipsize = TextUtils.TruncateAt.MIDDLE
3 -> textView.ellipsize = TextUtils.TruncateAt.END
4 -> textView.ellipsize = TextUtils.TruncateAt.MARQUEE
}
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_maxLines) {
textView.maxLines = a.getInt(attr, -1)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_background) {
QMUIViewHelper.setBackgroundKeepingPadding(textView, a.getDrawable(attr))
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_lineSpacingExtra) {
textView.setLineSpacing(a.getDimensionPixelSize(attr, 0).toFloat(), 1f)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_drawablePadding) {
textView.compoundDrawablePadding = a.getDimensionPixelSize(attr, 0)
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_textColorHint) {
textView.setHintTextColor(a.getColor(attr, 0))
} else if (attr == R.styleable.QMUITextCommonStyleDef_android_textStyle) {
val styleIndex = a.getInt(attr, -1)
textView.setTypeface(null, styleIndex)
}
}
textView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
a.recycle()
}
}
以上代码可以使主题属性在AS上预览xml布局时看到实时效果,目前表现是只有run app时才能看得到布局效果。这个解决了可以提高布局开发效率。