2017-12-27 324 views
2

我试图创建一个自定义视图(editText)类,这将有助于我设置leftDrawable并调整大小:设置leftDrawable宽度和高度自定义视图类

我的问题是:我想设置我的绘制对象,但没有任何反应,奇怪的事实是,我的绘制不为空:

我整个的自定义编辑文本类

public class FontableEditText extends android.support.v7.widget.AppCompatEditText { 

String TaFont; 
float LD_Width, LD_Height; 


public FontableEditText(Context context) { 
    super(context); 


} 

public FontableEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 


    Drawable drawable; 

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FontableEditText, 0, 0); 
    try { 

     TaFont = ta.getString(R.styleable.FontableEditText_ta_font); 
     LD_Width = ta.getDimension(R.styleable.FontableEditText_ld_width, 0); 
     LD_Height = ta.getDimension(R.styleable.FontableEditText_ld_height, 0); 
     drawable = ta.getDrawable(R.styleable.FontableEditText_ld_drawable); 


    } finally { 
     ta.recycle(); 
    } 

    setFont(TaFont); 

    if (drawable != null) { 
     Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); 
     Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, (int) LD_Width, (int) LD_Height, true)); 
     this.setCompoundDrawables(d, null, null, null); 
    } 

} 

void setFont(String FontName) { 


    try { 
     Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + FontName); 
     this.setTypeface(font); 
    } catch (Exception e) { 
     Log.v("Fontable View", e.getMessage(), new Throwable(e.getMessage())); 
    } 

} 

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

} 

我attr.xml

<declare-styleable name="FontableEditText"> 
     <attr name="ta_font" format="string"/> 
     <attr name="ld_drawable" format="reference"/> 
     <attr name="ld_width" format="dimension" /> 
     <attr name="ld_height" format="dimension"/> 
    </declare-styleable> 

我的XML孩子

<com.tarlanahad.kitabstore.Views.FontableEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="18dp" 
      android:layout_marginTop="40dp" 
      android:background="@drawable/sign_in_edit_text_border" 
      android:hint="[email protected]" 
      android:padding="10dp" 
      android:textColor="@color/white" 
      android:textColorHint="@color/white" 
      android:textSize="17sp" 
      app:ld_drawable="@drawable/sign_in_email_icon" 
      app:ld_height="40dp" 
      app:ld_width="40dp" 
      app:ta_font="Lato-Light.ttf" /> 
+0

您是否尝试直接设置drawable?没有Bitmaps的所有中间步骤 – elmorabea

回答

0

尝试从reference改变甲到integer

变化

<attr name="ld_drawable" format="reference"/> 

<attr name="ld_drawable" format="integer"/> 
+0

我也试过这个,没有任何反应...... –

相关问题