我正在使用具有自定义背景的按钮。
首先,我将按钮图像添加到了drawable文件夹中。然后,我在drawable中为按钮创建了一个名为large_button.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/large_buttom_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/large_buttom_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/large_buttom_default" />
</selector>
然后,我使用活动布局中的按钮:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test button Text"
android:onClick="sendMessage"
android:background="@drawable/large_button" />
问题是,(除了按钮比我想要的高),按钮上的文字全部大写:
我还想知道如何告诉它使用另一种字体(例如默认字体或已经可用的字体,不一定是我必须在ttf中导入的一种)
我将在另一个问题中解决按钮尺寸问题,这样,搜索其中一个问题的人们就不必阅读这两个问题,并且可能会感到困惑。
从xml更改字体的类型和样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAllCaps="false"
android:typeface="monospace"/>
这样文本就不会全部大写
android:textAllCaps="false"
如果您想从活动中更改字体
Button btn = (TextView) findViewById(R.id.btn);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
btn.setTypeface(font);
从您的活动中,您可以使用此功能
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof Button ) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句