0
我想知道什麼是最好的方法是做一個實心的方形按鈕,並添加一個自定義的字體它..我想着一個分離的類,其中繪製(確定寬度=高度取決於屏幕的寬度)。唯一我想知道的是:我怎樣才能讓它成爲一個帶有文本的按鈕?我是否在我的xml中放置了一個按鈕,是否可以用我自己繪製的按鈕方塊替換它?繪製實心方形按鈕,並添加自定義字體
謝謝!
我想知道什麼是最好的方法是做一個實心的方形按鈕,並添加一個自定義的字體它..我想着一個分離的類,其中繪製(確定寬度=高度取決於屏幕的寬度)。唯一我想知道的是:我怎樣才能讓它成爲一個帶有文本的按鈕?我是否在我的xml中放置了一個按鈕,是否可以用我自己繪製的按鈕方塊替換它?繪製實心方形按鈕,並添加自定義字體
謝謝!
如果你想定製原始按鈕,你可以通過android:background
添加圖像作爲你的按鈕背景。關於自定義字體,您在活動中獲取按鈕並設置自定義Typeface
。在assets
文件夾中放置您的字體。
Button txt = (Button) findViewById(R.id.button);
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");
txt.setTypeface(font);
如果你想使用這種按鈕的manytime您可以創建一個類似於此
的CustomButton
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
public class CustomButton extends Button {
private static final String TAG = "TextView";
public CustomButton (Context context) {
super(context);
}
public CustomButton (Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomButton (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomButton);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml一個自定義按鈕:(在res /值)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomButton ">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
main.xml中:使用HashMap來避免存儲器問題
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.CustomButton
android:id="@+id/button"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/showingOffTheNewTypeface"
foo:customFont="custom.ttf">
</com.example.CustomButton >
</LinearLayout>
字樣類。與tf= Typefaces.get(mContext, "cutomefont");
public class Typefaces{
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String name){
synchronized(cache){
if(!cache.containsKey(name)){
Typeface t = Typeface.createFromAsset(
c.getAssets(),
String.format("fonts/%s.OTF", name)
);
cache.put(name, t);
}
return cache.get(name);
}
}
}
Another tut更改setCustomFont
的tf = Typeface.createFromAsset(ctx.getAssets(), asset);
互聯網
謝謝,但我只是想知道,是不是有點怪(和慢得多)設置自定義按鈕,背景,因爲我只是想要一個堅實的正方形(沒有圓角或任何)?是不是更好更快(如果可能的話)從Java中繪製出一些東西,並使其成爲一個帶有文本的按鈕? –
我真的不明白你的意思(我明白你想要自定義按鈕)。 Android提供了組件'Button'。如果你想定製按鈕,你可以簡單地從xml中改變屬性。 Android會爲你抽籤。我只看到別人使用繪製一些複雜的組件。 –