Android按钮字体大小

时间:2010-05-13 00:51:28

标签: android

我一直在尝试使用本教程 - http://www.gersic.com/blog.php?id=56

在Android中创建自定义按钮

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

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

8 个答案:

答案 0 :(得分:93)

您可以像在其他任何位置一样在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) -->

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

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

答案 1 :(得分:9)

Button butt= new Button(_context);
butt.setTextAppearance(_context, R.style.ButtonFontStyle);

和res / values / style.xml

<resources>   
    <style name="ButtonFontStyle">
            <item name="android:textSize">12sp</item>
    </style>
</resources>

答案 2 :(得分:5)

编程:

Button bt = new Button(this);
bt.setTextSize(12);

在xml中:

<Button
    android:textSize="10sp"
/>

答案 3 :(得分:2)

我尝试将字体大小放在styles.xml中,但是当我去使用它时它只允许来自dimen文件夹的资源,所以把它放在那里,不知道这是对的

        <Button
                android:layout_weight="1"
                android:id="@+id/three_btn"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:onClick="onButtonClick"
                android:textColor="#EEEEEE"
                android:textStyle="bold"
                android:textSize="@dimen/buttonFontSize"
                android:text="3"/>

答案 4 :(得分:1)

另一种以编程方式处理;

final Button btn = (Button) findViewById(R.id.btnSize);

        final float[] size = {12};

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                size[0] +=2;
                btn.setTextSize(size[0] +2);
            }
        });

每次单击按钮时,按钮文本都会更改(+ 2px大小)。您可以添加另一个按钮并更改大小-2px。如果您想保存其他空缺的尺寸,可以使用共享首选项界面。

答案 5 :(得分:0)

<resource>
<style name="button">
<item name="android:textSize">15dp</item>
</style>
<resource>

答案 6 :(得分:0)

Another approach:

declaring an int first with the default fontsize

int txtSize = 16;

           button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mTextView.setTextSize(txtSize++);
                }
            });

答案 7 :(得分:0)

在XML文件中,可以通过添加来更改ant文本的字体大小,包括按钮,textView,editText等。

android:textSize =“ 10sp”

相关问题