繁体   English   中英

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

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

我想在多行按钮中设置2个不同的大小。

我现在正在使用以下方法,但是我无法控制大小,并且不知道在不同屏幕尺寸下的外观。

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

他们中有些人说而不是使用大或小。 使用html标签。 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>"));

下面是我的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" />

已编辑

所以现在我的代码变成

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

对于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);

您可以使用如下所示的内容:

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

这会将“单击此处”设置为粗体。 然后“排队”以在下一行加倍字体。

暂无
暂无

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

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