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会为你抽签。我只看到别人使用绘制一些复杂的组件。 –