从Custom TextView中的dimen xml文件访问值

问题描述

我有一个需求,我需要在App的所有模块之间使用自定义TextView。
用户将XML属性输入为“大”时,自定义textview应该从dimen.xml中获取值并将其设置为40sp。
我在执行此操作时遇到了问题:
请找到我的实现,如下所示:

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="textSize"/>
    </declare-styleable>

    <attr name="textSize" format="enum">
        <enum name="text_size_large" value="1"/>
        <enum name="text_size_small" value="2"/>
        <enum name="text_size_medium" value="3"/>
    </attr>
</resources>

dimens.xml:

<resources>
    <!-- Default screen margins,per the Android Design guidelines. -->
    <dimen name="text_size_small">16sp</dimen>
    <dimen name="text_size_medium">25sp</dimen>
    <dimen name="text_size_large">40sp</dimen>
</resources>

CustomTextView.kt

class CustomFontTextView   @JvmOverloads constructor(
    context: Context,attrs: AttributeSet? = null,defStyle: Int = 0,defStyleRes: Int = 0
) : AppCompatTextView(context,attrs,defStyle) {
    var customFont: String? = null

    init {
        attrs?.let {
            setTextSize(context,attrs);
        }
    }

    private fun setTextSize(context: Context,attrs: AttributeSet){
        val a = context.obtainStyledAttributes(
            attrs,R.styleable.CustomFontTextView
        )
        val cf = a.getInteger(R.styleable.CustomFontTextView_textSize,0)
        var fontSize = 1
        fontSize = when(cf){
            1 -> R.dimen.text_size_large
            2 -> R.dimen.text_size_small
            3 -> R.dimen.text_size_medium
            else -> R.dimen.text_size_small
        }
        println("fontSize $fontSize")
        val textSize  = a.getDimensionPixelSize(fontSize,0);
        println("TextSize $textSize")
        this.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize.toFloat());
        a.recycle()
    }

我的布局:

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.android.dynamicfeaturemodulesample.UI.CustomFontTextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="57dp"
        android:text="TextView"
        app:fontName="Roboto_Italic"
        app:textSize="text_size_large"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

收到以下异常:

2020-09-17 21:20:19.262 17486-17486/com.android.dynamicfeaturemodulesample 
E/AndroidRuntime: Caused by: java.lang.Arrayindexoutofboundsexception: 
length=875; index=2032337659 at 
android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:774)

有人可以帮助我解决此问题吗?

解决方法

您应将其编写如下:

 app:textSize="@dimen/text_size_large"