2017-08-11 138 views
0

TL; DR:使用上一個TextView簡單android:drawableLeft功能導致崩潰圖片旁邊的圖片?

TextView是Android的最基本的視覺元素之一。那爲什麼它最基本的功能不起作用?

https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft

我的代碼很簡單,它是使用圖像的自定義對話框旁邊TextView

<TextView 
     android:text="Text next to check mark" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:id="@+id/textView19" 
     android:drawableLeft="@drawable/ic_check_circle_white_24dp" 
     android:textSize="24sp" 
     android:textStyle="normal|bold"/> 

的代碼提供了可視化編輯器驚人的結果:

enter image description here

但是當程序實際上是ex ecuted,簡單android:drawableLeft功能崩潰的應用程序:

android.view.InflateException: Binary XML file line #64: Error inflating class TextView 

Full stacktrace


我嘗試打開該對話框,如下所示:

dialog = new Dialog(this,R.style.Theme_AppCompat_Light_Dialog_Alert); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.premium_dialog); 
dialog.setCanceledOnTouchOutside(false); 

dialog.getWindow().setLayout((int) (getResources().getDisplayMetrics().widthPixels * 0.90), (int) (getResources().getDisplayMetrics().heightPixels * 0.90)); 

dialog.show(); 

爲什麼說這樣的基本功能InflateException的原因? (我知道這是導致該問題的drawableLeft函數,因爲當我刪除TextView時,錯誤消失)

+0

從完整的堆棧跟蹤在那裏你用'造成的:android.content.res.Resources $ NotFoundException:文件** RES /繪製/ abc_dialog_material_background.xml **從顏色狀態列表資源ID#0x7f020011' –

+0

不能在較低API的'Textview'中直接使用'vectordrwable'。創建CustomTextview以支持向下API中的向量 –

回答

3

嘗試這一項上的setContentView

LayoutInflater layoutInflater = LayoutInflater.from(context); 
View promptView = layoutInflater.inflate(R.layout.premium_dialog, null); 
dialog.setContentView(promptView); 
+0

下載了複選標記。爲什麼這個工作? –

1

android:drawableLeft(和頂部/右/底部)不能在較舊的API級別上使用矢量繪製。可以使用.png drawable,或者在TextView旁邊使用帶有ImageView的LinearLayout。

+0

這是一個PNG,我從https://material.io/icons/#ic_check_circle –

3

創建下的API VectorDrawable一個CustomTextView。

用來初始化您的自定義的TextView的屬性如下:

private void initAttrs(Context context, AttributeSet attrs) { 
     if (attrs != null) { 
      TypedArray attributeArray = context.obtainStyledAttributes(attrs,R.styleable.CustomTextView); 
      //for font 
      String fontName = attributeArray.getString(R.styleable.CustomTextView_font); 

      try { 
       if (fontName != null) { 
        Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName); 
        setTypeface(myTypeface); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      Drawable drawableLeft = null; 
      Drawable drawableRight = null; 
      Drawable drawableBottom = null; 
      Drawable drawableTop = null; 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       drawableLeft = attributeArray.getDrawable(R.styleable.CustomTextView_drawableLeftCompat); 
       drawableRight = attributeArray.getDrawable(R.styleable.CustomTextView_drawableRightCompat); 
       drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat); 
       drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat); 
      } else { 
       final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableLeftCompat, -1); 
       final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableRightCompat, -1); 
       final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, -1); 
       final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, -1); 

       if (drawableLeftId != -1) 
        drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId); 
       if (drawableRightId != -1) 
        drawableRight = AppCompatResources.getDrawable(context, drawableRightId); 
       if (drawableBottomId != -1) 
        drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId); 
       if (drawableTopId != -1) 
        drawableTop = AppCompatResources.getDrawable(context, drawableTopId); 
      } 
      setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); 
      attributeArray.recycle(); 
     } 
    } 

您的自定義設置樣式爲相同。

<attr name="drawableLeftCompat" format="reference" /> 
    <attr name="drawableRightCompat" format="reference" /> 
    <attr name="drawableTopCompat" format="reference" /> 
    <attr name="drawableBottomCompat" format="reference" /> 

    <declare-styleable name="CustomTextView"> 
     <attr name="font" format="string" /> 
     <attr name="drawableLeftCompat" /> 
     <attr name="drawableRightCompat" /> 
     <attr name="drawableTopCompat" /> 
     <attr name="drawableBottomCompat" /> 
    </declare-styleable> 
+0

爲什麼?我只是從(https://material.io/icons/#ic_check_circle)處理簡單的.png,而不是向量!爲什麼我必須實現這個方法,如果基本功能是使用'drawableLeft'? –

+0

@Ruchir Banronia如果你的'drawableLeft'不是Vector _(哪個更具優勢和可伸縮性)_,則不需要創建自定義Textview來訪問.png文件。但是從你的stackTrace來看,它表明你正試圖訪問矢量繪圖。 –

+0

這就是爲什麼我很困惑。我從這裏使用.png文件:https://material.io/icons/#ic_check_circle。還有其他建議嗎? –