0

我想知道最好的方法是制作一个实心方形按钮并为其添加自定义字体。我正在考虑一个处理绘图的单独类(某些宽度=高度取决于屏幕的宽度)。我唯一想知道的是:如何使它成为带有文本的按钮?我是否在我的 xml 中放置一个按钮,是否可以用我自己绘制的按钮方块替换它?

谢谢!

4

1 回答 1

1

If you want to custom original button, you can add image as your button background via android:background. About custom font, You get button in activity and set custom Typeface.Put your font in assets folder.

Button txt = (Button) findViewById(R.id.button);  
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");  
txt.setTypeface(font);

If you want to use this kind of button manytime you can create a custom button class similar to this

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: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomButton ">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

main.xml:

<?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>

Typeface class using HashMap to avoid memory problem. Change the tf = Typeface.createFromAsset(ctx.getAssets(), asset); in setCustomFont with 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 in Internet

于 2012-09-28T02:31:31.130 回答