创建带有两行文本的自定义按钮 [英] Creating a customised button with 2 lines of text

查看:77
本文介绍了创建带有两行文本的自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手(Visual Studio已有20年了)。我需要创建一个可点击的控件,该控件具有2行文本(在按钮顶部为标题提供1个较小的字体,为值提供一个较大的字体-会发布图片,但我不允许这样做)。较大字体的大小会缩放,以使该值适合控件。
我很确定我需要对按钮控件进行子类化,但在这种情况下不知道该如何做。我发现的所有示例似乎都不符合要求。

I'm new to Android (Visual Studio for 20 years). I need to create a clickable control that features 2 lines of text (1 smaller font at the top of the button for a caption and a larger font line for a value - would post an image but I'm not allowed). The size of the larger font scales so that the value will fit on the control. I'm pretty sure I need to subclass the button control but not sure how to in this case. All samples I have found don't seem to fit the bill.

使用VB.Net可以轻松完成此操作,但是当我尝试使用Android时,我感到很困惑。任何帮助非常感谢。对于其他人也可能是一个方便的控件。
谢谢

Have done this easily with VB.Net but I'm stumped when I try with Android. Any help very much appreciated. Might be a handy control for others too. Thanks

推荐答案

您可以尝试在代码中使用Spannable,例如:

You could try using Spannable in the code like:

public class Test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button but = (Button) findViewById(R.id.button1);
        String butText= "Line 1\nLine 2";
        but.setText(formatString(butText));
    }

    private Spannable formatString(String str) {

        int startSpan = str.indexOf("\n");
        int endSpan   = str.length();
        Spannable spanString = null;
        spanString = new SpannableString(str);
        spanString.setSpan(new TextAppearanceSpan(this,
                R.style.custompoint), startSpan, endSpan,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);        
        return spanString;
    }

}

您有样式的'custompoint '

Where you have a style 'custompoint'

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style
        name="custompoint">
        <item name="android:textSize">24sp</item>
        <item name="android:textStyle">bold</item>
    </style>
</resources>

这篇关于创建带有两行文本的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆