Android 按钮字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2823808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:31:29  来源:igfitidea点击:

Android button font size

android

提问by jonhobbs

I;ve been trying to create a custom button in android using this tutorial - http://www.gersic.com/blog.php?id=56

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

It works well but it doesn't say how to change the font size or weighting. Any ideas?

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

There was another question on here and the only answer was to use html styling but you can't change a font size in html without using css (or the deprecated font tag). There must be a better way of setting the pixel size of the font used on buttons?

这里还有另一个问题,唯一的答案是使用 html 样式,但是如果不使用 css(或已弃用的字体标签),则无法更改 html 中的字体大小。必须有更好的方法来设置按钮上使用的字体的像素大小吗?

回答by Cheryl Simon

You define these attributes in xml as you would anything else, for example:

您可以像定义其他任何东西一样在 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) -->

You can find the allowed attributes in the api.

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

Or, if you want this to apply to all buttons in your application, create a style. See the Styles and Themesdevelopment documentation.

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

回答by ashish

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

and in res/values/style.xml

并在 res/values/style.xml

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

回答by BRap

Programmatically:

以编程方式:

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

In xml:

在 xml 中:

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

回答by Jock Mahon

I tried to put the font size in the styles.xml but when i went to use it it was only allowing resources from the dimen folder so put it in there instead, dont know it this is right

我试图将字体大小放在 style.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"/>

回答by Umut D.

Another programmatically approach;

另一种编程方法;

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);
            }
        });

Every time you click your button, Button text will be change (+2px size). You can add another button and change size -2px too. If you want to save size for another openings, you may use Shared Preferenceinterface.

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

回答by Iura

Another approach:

另一种方法:

declaring an int first with the default fontsize

首先使用默认字体大小声明一个 int

int txtSize = 16;

int txtSize = 16;

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

回答by ANUSHKA GUPTA

In XML file, the font size of ant text including the button, textView, editText and others can be changed by the adding

在 XML 文件中,可以通过添加改变蚂蚁文本的字体大小,包括按钮、textView、editText 等

android:textSize="10sp"

android:textSize="10sp"

回答by sunny

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