【发布时间】:2016-04-04 05:59:23
【问题描述】:
我将在我的 xml 文件中使用的自定义按钮:
我想自定义一个矩形形状的按钮(来自 Material Design 库) 并为其添加自定义字体。
这就是我想出的:
import com.gc.materialdesign.views.ButtonRectangle;
public class FontCustomButton extends Button {
public FontCustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
MyButton myButton = new MyButton(context,attrs);
Typeface typeface = myButton.getFont(context);
this.setTypeface(typeface);
}
//inner class
public class MyButton extends ButtonRectangle{
public MyButton(Context context) {
super(context, attrs);
}
//this Method will return my font as TypeFace
public Typeface getFont(Context context){
Typeface face=Typeface.createFromAsset(context.getAssets(),"fonts/font_1.ttf");
return face;
}
}
}
这是我希望它被称为的方式:
<FontCustomButton
android:layout_width="140dp"
android:layout_height="60dp"
android:text="@string/log_in"
android:id="@+id/btn_login"
/>
现在我的按钮应该同时具有矩形形状和字体。
我使用的 Material Design 库的链接:
【问题讨论】: