Android按钮字体大小

jon*_*bbs 37 android

我一直在尝试使用本教程在Android中创建一个自定义按钮 - http://www.gersic.com/blog.php?id=56

它运作良好,但没有说明如何更改字体大小或权重.有任何想法吗?

这里还有另一个问题,唯一的答案是使用html样式但是你不能在不使用css(或不推荐的字体标签)的情况下更改html中的字体大小.必须有更好的方法来设置按钮上使用的字体的像素大小?

Che*_*mon 94

您可以像在其他任何位置一样在xml中定义这些属性,例如:

<Button android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            android:background="@drawable/mybutton_background"
            android:textSize="10sp" /> <!-- Use SP(Scale Independent Pixel) -->
Run Code Online (Sandbox Code Playgroud)

您可以在api中找到允许的属性.

或者,如果您希望将其应用于应用程序中的所有按钮,请创建样式.请参阅样式和主题开发文档.

  • 文本化不应设置为SP而不是DP吗?请参阅http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android (11认同)

小智 9

Button butt= new Button(_context);
butt.setTextAppearance(_context, R.style.ButtonFontStyle);
Run Code Online (Sandbox Code Playgroud)

在res/values/style.xml中

<resources>   
    <style name="ButtonFontStyle">
            <item name="android:textSize">12sp</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)


BRa*_*Rap 5

编程方式:

Button bt = new Button(this);
bt.setTextSize(12);
Run Code Online (Sandbox Code Playgroud)

在xml中:

<Button
    android:textSize="10sp"
/>
Run Code Online (Sandbox Code Playgroud)