这是我的代码(细分为相关部分): CustomTextView.classpublic class CustomTextView extends TextView { private float mHeight; private final String TAG = "CustomTextView"; private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance(); public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, "in CustomTextView constructor"); TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); this.mHeight = values.getDimension(R.styleable.CustomTextView_cHeight, 20); } @Override public void setText(CharSequence text, BufferType type) { Log.d(TAG, "in setText function"); Spannable s = getCustomSpannableString(getContext(), text); super.setText(s, BufferType.SPANNABLE); } private static Spannable getCustomSpannableString(Context context, CharSequence text) { Spannable spannable = spannableFactory.newSpannable(text); doSomeFancyStuff(context, spannable); return spannable; } private static void doSomeFancyStuff(Context context, Spannable spannable) { /*Here I'm trying to access the mHeight attribute. Unfortunately it's 0 though I set it to 24 in my layout and it's correctly set in the constructor*/ }} styles.xml<declare-styleable name="CustomTextView"> <attr name="cHeight" format="dimension"/></declare-styleable> layout.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ctvi="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.mypackage.views.CustomTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/my_fancy_string" android:textSize="16sp" ctvi:cHeight="24dp" /></LinearLayout>作为证明-这是LogCat的输出:30912-30912/com.mypackage.views D/CustomTextView﹕ in setText function30912-30912/com.mypackage.views D/CustomTextView﹕ in CustomTextView constructor因此您可以看到setText()方法在构造函数之前被调用.有点奇怪,我不知道要在setText方法中使用自定义属性(cHeight),需要更改什么.在此先感谢您的帮助!解决方案是TextView super()构造函数,它根据属性值调用setText().如果在设置文本值时确实需要访问自定义属性,请对文本也使用自定义属性.I've got a problem with my CustomTextView. I'm trying to get a custom value from my layout-xml file and use this in my setText() method. Unfortunately the setText() method gets called before the constructor and because of this I can't use the custom value in this method.Here's my code (broken down to the relevant parts):CustomTextView.classpublic class CustomTextView extends TextView { private float mHeight; private final String TAG = "CustomTextView"; private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance(); public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, "in CustomTextView constructor"); TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); this.mHeight = values.getDimension(R.styleable.CustomTextView_cHeight, 20); } @Override public void setText(CharSequence text, BufferType type) { Log.d(TAG, "in setText function"); Spannable s = getCustomSpannableString(getContext(), text); super.setText(s, BufferType.SPANNABLE); } private static Spannable getCustomSpannableString(Context context, CharSequence text) { Spannable spannable = spannableFactory.newSpannable(text); doSomeFancyStuff(context, spannable); return spannable; } private static void doSomeFancyStuff(Context context, Spannable spannable) { /*Here I'm trying to access the mHeight attribute. Unfortunately it's 0 though I set it to 24 in my layout and it's correctly set in the constructor*/ }}styles.xml<declare-styleable name="CustomTextView"> <attr name="cHeight" format="dimension"/></declare-styleable>layout.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ctvi="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.mypackage.views.CustomTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/my_fancy_string" android:textSize="16sp" ctvi:cHeight="24dp" /></LinearLayout>And just as a proof - here's the LogCat output:30912-30912/com.mypackage.views D/CustomTextView﹕ in setText function30912-30912/com.mypackage.views D/CustomTextView﹕ in CustomTextView constructorSo as you can see the setText() method is called before the constructor. That's kinda weird and I don't know what I need to change in order to use my custom attribute (cHeight) in the setText-method.Thanks in advance for any help! 解决方案 It's the TextView super() constructor that calls your setText() based on the attribute values.If you really need to access your custom attribute when setting a text value, use a custom attribute for the text as well. 这篇关于自定义TextView-在构造函数之前调用的setText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 17:21