我创建了从 TextView
扩展而来的简单自定义 View ,在 Android Studio 中我得到了这个警告
This custom view should extend android.support.v7.widget.AppCompatTextView instead
我不能使用 clickable
属性,例如:
<com.myapp.test.Widgets.FontAwesome
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="?selectableItemBackground"
android:gravity="center"
android:clickable="@{()->presenter.clickOnSend()}"
android:text="@string/font_icon_post_message"
android:textColor="@color/gray_text_color"
android:textSize="40sp"/>
clickable
属性出现此错误:
Error:(91, 46) Cannot find the setter for attribute 'android:clickable' with parameter type lambda on com.myapp.test.Widgets.FontAwesome.
我的自定义类:
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontAwesome extends TextView {
public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesome(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/fontawesome.ttf");
setTypeface(tf);
}
}
我该如何解决这个问题?
最佳答案
更新:如果您使用 androidx 库 而不是(旧版)v7 支持库(您现在应该这样做...),请改用这个:
import androidx.appcompat.widget.AppCompatTextView;
旧答案:(如果您还没有迁移到 androidx,仍然很有用...)
This custom view should extend android.support.v7.widget.AppCompatTextView instead
这是一个警告
,而不是一个错误。
代替
public class FontAwesome extends TextView
你应该使用 AppCompatTextView
public class FontAwesome extends AppCompatTextView
关于Android 自定义 View 应该扩展 AppCompatTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246336/