Dol*_*rma 3 android android-widget
我写了简单的小部件作为textview与一些属性,我想在扩展的TextView类中设置字体,如何做这个动作,我可以有这种能力?
atributes:
<resources>
<declare-styleable name="TextViewStyle">
<attr name="selected_background" format="integer" />
<attr name="font" format="string" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
自定义textview小部件:
public class TextViewStyle extends TextView{
public TextViewStyle(Context context) {
super(context);
}
public TextViewStyle(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewStyle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewStyle, defStyle, 0);
a.recycle();
}
}
Run Code Online (Sandbox Code Playgroud)
简单的UI小部件到xml:
<ir.jaziire.widgets.TextViewStyle
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/topic"
android:textColor="#000"
android:textSize="14dp"
android:gravity="right"
/>
Run Code Online (Sandbox Code Playgroud)
在这个小部件中,我想设置app:font=""为从资产设置任何字体
CustomTextView:
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
import com.androidhub.R;
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
try {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.font, defStyle, 0);
String str = a.getString(R.styleable.font_fonttype);
a.recycle();
switch (Integer.parseInt(str)) {
case 0:
str = "fonts/Trebuchet_MS.ttf";
break;
default:
break;
}
setTypeface(FontManager.getInstance(getContext()).loadFont(str));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@SuppressWarnings("unused")
private void internalInit(Context context, AttributeSet attrs) {
}
Run Code Online (Sandbox Code Playgroud)
FontManager:
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Typeface;
public class FontManager {
private Map<String, Typeface> fontCache = new HashMap<String, Typeface>();
private static FontManager instance = null;
private Context mContext;
private FontManager(Context mContext2) {
mContext = mContext2;
}
public synchronized static FontManager getInstance(Context mContext) {
if (instance == null) {
instance = new FontManager(mContext);
}
return instance;
}
public Typeface loadFont(String font) {
if (false == fontCache.containsKey(font)) {
fontCache.put(font,
Typeface.createFromAsset(mContext.getAssets(), font));
}
return fontCache.get(font);
}
}
Run Code Online (Sandbox Code Playgroud)
在attrs.xml文件中:
<declare-styleable name="font">
<attr name="fonttype">
<enum name="trebuchet_ms" value="0" />
</attr>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
使用你CustomTextView的xml:
在你的顶部声明这个 xml
xmlns:custom="http://schemas.android.com/apk/res/com.androidhub"
Run Code Online (Sandbox Code Playgroud)
你可以使用你CustomTextView的:
<com.utils.CustomTextView
android:id="@+id/loadMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/custom_button_selector"
android:clickable="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:padding="10dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/load_map"
android:textColor="@color/home_buttons_selector"
android:textSize="16sp"
custom:fonttype="trebuchet_ms" />
Run Code Online (Sandbox Code Playgroud)
确保您正在放置字体 assests-->fonts-->YourFont
com.androidhub 是我的包名.
| 归档时间: |
|
| 查看次数: |
3565 次 |
| 最近记录: |