2017-04-06 106 views
11

我創建了從0​​擴展,Android Studio中我得到這個wanrningAndroid的自定義視圖應該延伸AppCompatTextView

This custom view should extend android.support.v7.widget.AppCompatTextView instead 

,我不能使用clickable propertise,例如簡單的自定義視圖:

<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 propertise:

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); 
    } 
} 

我怎樣才能解決這個問題呢?

+0

'延長android.support.v7.widget.AppCompatTextView'是與TextView相同。 AppCompatTextView用於向舊版Android提供新功能的向後兼容性。 –

+0

thois不是問題,你可以擴展你想要的任何東西。 Android Studio只會警告您,AppCompatTextView更適合用於兼容性。 –

+0

該代碼仍然會運行....它只是一個警告 – rafsanahmad007

回答

7

該自定義視圖應該延伸 android.support.v7.widget.AppCompatTextView代替

這是一個Warning。不是錯誤。如果您使用AppCompatTextView而不是TextView會更好。

不要

public class FontAwesome extends TextView 

好辦法

您應該使用AppCompatTextView

public class FontAwesome extends AppCompatTextView 
+11

爲什麼?你能解釋一下爲什麼一個好,另一個不好。 – shadox

+4

爲了支持諸如着色之類的功能,AppCompat庫將自動爲內置小部件加載特殊的AppCompat替換項。但是,這不適用於您自己的自定義視圖。不是直接擴展android.widget類,而應該擴展android.support.v7.widget.Appcompat –

+8

中的一個委託類@Sachin他可以從警告本身讀取 –