2017-08-11 9 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の原因(TextViewを削除するとエラーがなくなるため、問題の原因となっているdrawableLeft関数です)

+0

色状態リストリソースのID番号の0x7f020011'から** **のres /描画可能/ abc_dialog_material_background.xmlファイル –

+0

'vectordrwable'をより低いAPIの' Textview'で直接使用することはできません。下位APIのベクトルをサポートするCustomTextviewを作成する –

答えて

3

はsetContentView

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

からチェックマークをダウンロードしました。なぜこれは動作しますか? –

1

android:drawableLeft(およびtop/right/bottom)は、古いAPIレベルでベクタードロワブルを使用できません。 .pngドロアブルを使用するか、TextViewの横にあるImageViewでLinearLayoutを使用してください。

+0

PNGです。https://material.io/icons/#ic_check_circle –

3

に、このいずれかを試してみてください下のAPIにVectorDrawableためCustomTextViewを作成します。次のようにカスタムのTextViewのあなたの属性Initilize

:同じのため

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

カスタムStyleable。 android.content.res.Resources $ NotFoundException:あなたは `によって引き起こさを使用しているあなたの完全なスタックトレースから

<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

なぜですか?私は単純な.png(https://material.io/icons/#ic_check_circle)からではなく、ベクトルを扱っています!基本的な機能が 'drawableLeft'だけを使用する場合、なぜこのメソッドを実装する必要がありますか? –

+0

@Ruchir Banron @あなたの 'drawableLeft'がVector _(これはもっと優先されスケーラブルです)_ではない場合、.pngファイルにアクセスするためにカスタムTextviewを作成する必要はありません。しかし、あなたのstackTraceから、あなたはベクトル描画にアクセスしようとしていることを示しています。 –

+0

それは私がとても混乱している理由です。私はここから.pngファイルを使用しています:https://material.io/icons/#ic_check_circle。その他の提案はありますか? –

関連する問題