我正在尝试将右侧绘制设置为自定义微调器类,它扩展了 MaterialAutoCompleteTextView 。在我的构造函数中,我可以使用来自自定义属性的drawable设置图标变量。但是,当我调试时,icon在 setCompoundDrawablesWithIntrinsicBounds 方法中为null。是因为它是在我在构造函数中设置自定义属性之前调用的吗?我该如何设置此值?你能帮帮我吗?
我的自定义微调器类如下:
public class CustomSpinner extends MaterialAutoCompleteTextView {
Drawable icon;
public CustomSpinner (Context context) {
super(context);
setOnItemClickListener(this);
}
public CustomSpinner (Context arg0, AttributeSet arg1) {
super(arg0, arg1);
TypedArray a = arg0.obtainStyledAttributes(arg1, R.styleable.CustomSpinner);
Drawable drawable = a.getDrawable(R.styleable.CustomSpinner_iconArrow);
if (drawable != null)
icon=drawable;
a.recycle();
setOnItemClickListener(this);
}
public CustomSpinner (Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
setOnItemClickListener(this);
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
if(icon!=null) {
right=icon;
}
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}
}
这是我的attrs.xml:
<resources>
<declare-styleable name="CustomSpinner">
<attr name="iconArrow" format="integer"/>
</declare-styleable>
</resources>
以下是我在布局中使用它的方法:
<com.deneme.test.CustomSpinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconArrow="@drawable/arrowdown"/>