简体   繁体   English

android在按钮中设置2种不同的字体大小

[英]android set 2 different font size in a button

i want to set 2 different size in a multiline button. 我想在多行按钮中设置2个不同的大小。

what i do now is using below method but i have no control over the size and does not know how it looks like on different screen size. 我现在正在使用以下方法,但是我无法控制大小,并且不知道在不同屏幕尺寸下的外观。

  final Button btnreset = (Button) findViewById(R.id.resetQ);
    btnreset.setText(Html.fromHtml("<big>Click Here</big><br/><small>To Queue</small>"));

some of them say instead of using big or small. 他们中有些人说而不是使用大或小。 use html tag. 使用html标签。 html tag works fine on the color attribute but not size attribute. html标记在color属性上工作正常,但在size属性上工作不正常。

  final Button btnreset = (Button) findViewById(R.id.resetQ);
    btnreset.setText(Html.fromHtml("<font size='9'>Click Here</font><br/><font size='2'>To Queue</font>"));

below is my xml coding 下面是我的xml编码

<Button
                   android:id="@+id/resetQ"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="50dp"                     
                   android:text="New Queue"
                       android:textSize="@dimen/lblsize"    
                     android:layout_weight="1" />

Edited 已编辑

so now my code becomes 所以现在我的代码变成

  Spannable spannable = new SpannableString("Click Here To Queue");
    spannable.setSpan(new RelativeSizeSpan(1.5f), 0, 10,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
   btnreset.setText(spannable);

For Android 5.0 (Lollipop) "or newer" add setTransformationMethod(null) to your button (see this link ). 对于Android 5.0(Lollipop)“或更高版本”,将setTransformationMethod(null)添加到您的按钮中(请参阅此链接 )。

Spannable span = new SpannableString(text + "\n" +  underText);
span.setSpan(new AbsoluteSizeSpan(12, true), text.length()+1, text.length()+1+underText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Button button = new Button(ctx);
button.setTextSize(16);
button.setText(span);
button.setTransformationMethod(null);

You can use something like this : 您可以使用如下所示的内容:

Spannable spannable = new SpannableString("Click here\nto queue");
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 10,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(2f), 11, 20,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
btnreset.setText(spannable);

This sets "Click here" to Bold. 这会将“单击此处”设置为粗体。 Then "to queue" to double size font on the next line. 然后“排队”以在下一行加倍字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM