使用自定义 textSize 实现自定义 TextView

Implement a custom TextView with custom textSize

我已经实现了一个自定义 TextView,然后我需要更改它的文本大小以供将来使用。在那一步,我决定使用 style.xml 和 attr.xml

自定义其 textSize

但是,当我的自定义布局膨胀时出现运行时错误。该错误与我为自定义而更改的部分有关。

这是我实现的代码片段。

attr.xml

<declare-styleable name="FieldLayout">
    <attr name="right_detail_text_size" format="dimension"/>
</declare-styleable>

styles.xml

 <style name="TextViewRightDetail">
    <item name="android:textSize">?attr/right_detail_text_size</item>
 </style>

layout_field_detailed.xml

    <com.my.view.TextViewFont
        android:id="@+id/layout_fielddetailed_textview_rightdetail"
        style="@style/TextViewRightDetail" />

layout_implementation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iattr="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.my.view.FieldLayoutDetailed
        android:id="@+id/fieldlayoutdetailed_example"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        iattr:right_detail_text_size="16sp"/>
</LinearLayout>

FieldLayoutDetailed.java

public FieldLayoutDetailed(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.FieldLayout, 0, 0);
    LayoutInflater.from(context).inflate(R.layout.layout_field_deailed, this, true);

我在运行时遇到的错误

...
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at android.widget.TextView.<init>(TextView.java:1003)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at com.my.view.TextViewFont.<init>(TextViewFont.java:29)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.my.view.FieldLayoutDetailed.<init>(FieldLayoutDetailed.java:50)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.my.ActivityWithMenu.setContentView(ActivityWithMenu.java:77)
at com.my.FastCreditApplyMain.onCreate(FastCreditApplyMain.java:37)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access0(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

现在我通过动态改变文字大小解决了问题,但我很好奇问题的原因。

谢谢。

这是你的问题:

Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
    at com.my.view.TextViewFont.<init>(TextViewFont.java:29)

可能的解决方案:将其放入 TextViewFont.java

的构造函数中
TypedArray a = context.obtainStyledAttributes(
        attrs,
        R.styleable.FieldLayout,
        0, 0);

try {
    final int fontSize = a.getDimensionPixelSize(R.styleable.FieldLayout_right_detail_text_size, 0);
    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
}

finally {
    a.recycle();
}

TypedArray a = context.obtainStyledAttributes(
        attrs,
        R.styleable.FieldLayout,
        0, 0);

try {
        final int fontSize = a.getDimension(R.styleable.FieldLayout_right_detail_text_size, 0);
        this.setTextSize(fontSize);
}

finally {
    a.recycle();
}