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

时间:2014-10-02 02:22:09

标签: android html

我想在多行按钮中设置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);

2 个答案:

答案 0 :(得分:2)

对于Android 5.0(Lollipop)&#34;或更新的&#34; 将setTransformationMethod(null)添加到按钮(请参阅此link)。

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

答案 1 :(得分:1)

您可以使用以下内容:

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

这将“点击此处”设置为Bold。然后在下一行“排队”到双倍大小的字体。