瀏覽代碼

Font Widgets

master
gitsvn 12 年之前
父節點
當前提交
f878d04d5f
共有 3 個文件被更改,包括 81 次插入4 次删除
  1. 4
    3
      res/layout/rbs_main.xml
  2. 9
    1
      res/values/attrs.xml
  3. 68
    0
      src/ch/spherIC/recurvebowsight/components/FontEditText.java

+ 4
- 3
res/layout/rbs_main.xml 查看文件

@@ -143,7 +143,7 @@
android:textColor="@color/black"
app:font="@string/TitilliumWeb_Regular" />
<EditText
<ch.spherIC.recurvebowsight.components.FontEditText
android:id="@+id/arrowDiameterTxtFld"
android:layout_width="150dp"
android:layout_height="36dp"
@@ -153,9 +153,10 @@
android:inputType="numberDecimal"
android:paddingBottom="9dp"
android:paddingTop="9dp"
android:text="22"
android:text="0123456789"
android:textSize="18sp"
android:typeface="sans" />
android:typeface="sans"
app:txtFont="@string/TitilliumWeb_Regular" />
</TableRow>

+ 9
- 1
res/values/attrs.xml 查看文件

@@ -25,7 +25,15 @@
</declare-styleable>
<declare-styleable
<declare-styleable
name="FontEditText">
<attr
name="txtFont"
format="string" />
</declare-styleable>
<declare-styleable
name="FontButton">
<attr

+ 68
- 0
src/ch/spherIC/recurvebowsight/components/FontEditText.java 查看文件

@@ -0,0 +1,68 @@
/**
* Copyright (C) 2012 - Florindo Smilari (spherIC)
*/
package ch.spherIC.recurvebowsight.components;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import ch.spherIC.recurvebowsight.R;
/**
* Siehe http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android.
*
* @author FC Smilari
*/
public class FontEditText extends EditText {
private static final String TAG = "EditText";
public FontEditText(final Context context) {
super(context);
}
public FontEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
setFont(context, attrs);
}
public FontEditText(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
setFont(context, attrs);
}
private void setFont(final Context ctx, final AttributeSet attrs) {
if (!isInEditMode()) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontEditText);
String font = a.getString(R.styleable.FontEditText_txtFont);
if (font != null && !font.isEmpty()) {
setFont(ctx, font);
}
a.recycle();
}
}
public boolean setFont(final Context ctx, final String font) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), font);
} catch (final Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}

Loading…
取消
儲存