Last active
August 29, 2015 14:00
-
-
Save AbeHaruhiko/11083133 to your computer and use it in GitHub Desktop.
Androidで画像を使わずにオシャレなアイコンを簡単に表示する方法(Font Awesome) ref: http://qiita.com/AbeHaruhiko/items/a7437027231a373acf2a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="CustomFontButton"> | |
<attr name="customFont" format="string"/> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.util.Log; | |
import java.util.Hashtable; | |
public class CachedTypefaces { | |
private static final String TAG = "CachedTypefaces"; | |
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); | |
public static Typeface get(Context context, String assetPath) { | |
synchronized (cache) { | |
if (!cache.containsKey(assetPath)) { | |
try { | |
Typeface t = Typeface.createFromAsset(context.getAssets(), assetPath); | |
cache.put(assetPath, t); | |
} catch (Exception e) { | |
Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage()); | |
return null; | |
} | |
} | |
return cache.get(assetPath); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:myapp="http://schemas.android.com/apk/res-auto" | |
<!-- ↑xmlnsの追加を忘れずに! --> | |
android:id="@+id/LinearLayout01" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical"> | |
<your.app.package.CustomFontButton | |
android:id="@+id/btnSomething" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="" | |
android:textSize="30sp" | |
android:textColor="#666" | |
myapp:customFont="fontawesome-webfont.ttf" | |
android:onClick="onClickSomething"/> | |
</LinearLayout> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment