Android 自定义 View 应该扩展 AppCompatTextView

标签 android

我创建了从 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. 

enter image description here

我的自定义类:

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/

相关文章:

android - Gradle sync failed : The android gradle plugin version 2. 3.0-beta1太旧,请更新到最新版本

android - 原生 Kotlin 库是否可以在 Xamarin 中使用

android - 如何显示本地 ://image in ionic framework + cordova?

android - 如何将 Object[] 转换为 String[ ]

java - 如何从 Android 应用程序发送文件

android - 在Firebase-Realtime Database上使用什么规则让用户注册和登录?

android - 如何让滑动面板中的元素监听点击事件

android - 无法在某些 Android 手机上使用 SmsManager 发送短信

java - Android list 困惑

android - 如何在 Capacitor 中使用 cordova-res 生成图标