내 모든 EditText에 대해 내 양식 전체에 다른 스타일의 새 글꼴을 적용하고 싶습니다. 나는 아래의 EditText를 확장 CustomEditText 클래스를 생성하여 그렇게 노력 :Android에서 사용자 정의 EditText 만들기
public class CustomEditText extends EditText {
public static String LATO_BOLD = "fonts/Lato-Bold.ttf";
public static String LATO_LIGHT = "fonts/Lato-Light.ttf";
public static String LATO_REGULAR = "fonts/Lato-Regular.ttf";
public static String LATO_BLACK = "fonts/Lato-Black.ttf";
private static final int BOLD = 1;
private static final int LIGHT = 2;
private static final int REGULAR = 3;
private static final int BLACK = 4;
public CustomEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init();
}
public CustomEditText(Context context) {
this(context, null);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode())
return;
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.com_pts_mm_android_views_CustomEditText, 0, 0);
final int fontType = a.getInt(0, -1);
a.recycle();
switch (fontType) {
case LIGHT:
setLight();
break;
case BOLD:
setBold();
break;
case REGULAR:
init();
break;
case BLACK:
setBlack();
break;
default:
init();
break;
}
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
LATO_REGULAR);
this.setTypeface(tf);
}
public void setLight() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
LATO_LIGHT);
this.setTypeface(tf);
}
public void setBold() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
LATO_BOLD);
this.setTypeface(tf);
}
public void setBlack() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
LATO_BLACK);
this.setTypeface(tf);
}
}
나는이 내 모든 글꼴 파일 (TTF 파일) 자산/글꼴 폴더/아래. 그런 다음 속성과 stylables 아래로 attrs.xml이에 정의되어 있습니다
<attr name="custom_font">
<enum name="bold" value="1" />
<enum name="light" value="2" />
<enum name="regular" value="3" />
<enum name="black" value="4" />
</attr>
<declare-styleable name="com.pts.mm.android.views.CustomEditText">
<attr name="custom_font" />
</declare-styleable>
을 그래서 지금은 아래와 같이 내 레이아웃이 사용자 정의 글고 치기를 사용합니다 :
<com.pts.mm.android.views.CustomEditText
android:id="@+id/etNric"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/edit_box_horizontal_padding"
android:paddingRight="@dimen/edit_box_horizontal_padding"
android:layout_marginBottom="@dimen/paragraph_vertical_margin"
android:textSize="@dimen/text_edit_size"
android:textColor="@color/text_edit_color"
android:inputType="text"
android:background="@drawable/textbox_userid1"
custom_font="regular"/>
그러나이 작동하지 않습니다. 내 EditText 필드를 편집 할 수 없게됩니다 (커서가 사라집니다). 누구가 잘못되었는지 어떤 단서가 있습니까? 감사.
editText xml에서 'custom_font'의 접두어는 어디에 있습니까? –
OK,이 명령어 [link] (http://developer.android.com/training/custom-views/create-view.html)에 따라 네임 스페이스를 정의하려했지만 작동하지 않습니다. –