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

前端之家收集整理的这篇文章主要介绍了android – 使用自定义textSize实现自定义TextView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实现了自定义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);
    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.access$800(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,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,0);

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

finally {
    a.recycle();
}
原文链接:https://www.f2er.com/android/316055.html

猜你在找的Android相关文章