【问题标题】:Android custom view should extend AppCompatTextViewAndroid 自定义视图应该扩展 AppCompatTextView
【发布时间】:2017-04-06 05:23:06
【问题描述】:

我创建了从TextView 扩展的简单自定义视图,在 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);
    }
}

我该如何解决这个问题?

【问题讨论】:

  • extend android.support.v7.widget.AppCompatTextView 与 TextView 相同。 AppCompatTextView 用于为旧版 Android 的新功能提供向后兼容性。
  • thois 不是问题,你可以扩展任何你想要的。 Android Studio 只警告你 AppCompatTextView 更适合用于兼容性。
  • 代码仍然会运行......这只是一个警告
  • 是否启用数据绑定?

标签: android


【解决方案1】:

更新:如果您使用 androidx 库 而不是(旧版)v7 支持库(您现在应该这样做...),请改用这个:

import androidx.appcompat.widget.AppCompatTextView;

旧答案:(如果您还没有迁移到 androidx,仍然很有用...)

这个自定义视图应该扩展 android.support.v7.widget.AppCompatTextView 代替

这是Warning,而不是错误。

代替

public class FontAwesome extends TextView

你应该使用AppCompatTextView

public class FontAwesome extends AppCompatTextView 

【讨论】:

  • 为什么?你能解释一下为什么一个是好的,另一个不是。
  • 所以,如果我们不需要任何 backgroundTint 或 autoSize 功能,我们仍然可以从 TextView 扩展 customView,对吧?
  • @KimiChiu 是的,但最好使用 AppCompatTextView,这样如果您决定在线发布视图或想要使用任何新属性
【解决方案2】:

我遇到了类似的问题,但已解决。可能您第一次只在执行时看到结果。我没有在没有启动应用程序的情况下再次测试重建项目。

 import android.content.Context;
 import android.graphics.Color;
 import android.support.v7.widget.AppCompatTextView;
 import android.util.AttributeSet;

 public class CustomTxtView extends AppCompatTextView {

    public CustomTxtView(Context context) {
        super(context);
        init();
    }

    public CustomTxtView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTxtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setText("Hello World");
        setTextColor(Color.RED);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多