【问题标题】:Creating a customised button with 2 lines of text创建带有 2 行文本的自定义按钮
【发布时间】:2011-03-10 12:19:44
【问题描述】:

我是 Android 新手(Visual Studio 20 年)。我需要创建一个具有 2 行文本的可点击控件(按钮顶部的 1 个较小的字体用于标题,而较大的字体行用于值 - 将发布图像,但我不允许)。较大字体的大小会缩放,以便该值适合控件。 我很确定我需要对按钮控件进行子类化,但不确定在这种情况下如何。我找到的所有样品似乎都不符合要求。

使用 VB.Net 很容易做到这一点,但是当我尝试使用 Android 时,我感到很困惑。非常感谢任何帮助。对其他人来说也可能是一个方便的控制。 谢谢

【问题讨论】:

  • 你想要看起来像按钮的控件还是只是可点击的控件?

标签: android button controls customization


【解决方案1】:

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

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'

<?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>

【讨论】:

  • 我试过了,这行得通,不知道为什么 OP 不接受它作为答案。
  • @NickT 如果我将 html.fromHtml 与可扩展字符串一起使用,它不起作用,效果消失了吗??
猜你喜欢
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多