使用自定义视图类设置leftDrawable宽度和高度

问题描述 投票:2回答:1

我正在尝试创建一个自定义视图(editText)类,它将帮助我设置leftDrawable并调整它的大小:

我的问题是:我正在尝试设置我的Drawable但没有任何反应,奇怪的事实是我的drawable不是null:

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

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" />
android android-custom-view
1个回答
0
投票

尝试将格式从qazxsw poi更改为qazxsw poi

更改

reference

integer
© www.soinside.com 2019 - 2024. All rights reserved.