0

我有一个要求,我需要在 App 的所有模块中使用自定义 TextView 。
当用户输入 XML 属性为“大”时,自定义文本视图应从 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>

尺寸.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>

自定义文本视图.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)

谁能帮我解决这个问题?

4

1 回答 1

0

你应该这样写:

 app:textSize="@dimen/text_size_large"
于 2020-09-17T19:37:31.450 回答