如何在 Android 拨号器应用程序中的 TextView 中添加大小写字母?

How can I add big AND small letters in a TextView like in the Android Dialer app?

如何在 TextView 中添加大 AND 小字母,如下图所示:

抱歉:"nuber" = "number"

您可以尝试使用 Html.fromHtml 作为:

button.setText(Html.fromHtml("html_code_with"));

而不是 html_code_with 为大文本和小文本设置 html 代码。

您在 UI 中看到的按钮似乎不是标准 Android 按钮,而是由两个 TextView 组成的复合视图。

您应该确保您的复合视图是可聚焦和可点击的(而不是其组成的两个 TextView)并为此视图提供默认、聚焦和按下状态。

public class MyCompositeView extends LinearLayout {

    private TextView primaryTextView;
    private TextView secondaryTextView;

    public MyCompositeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setOrientation(HORIZONTAL);
    }

    @Override
    public final void setOrientation(int orientation) {
        if (orientation != HORIZONTAL) {
            throw new IllegalArgumentException("MyCompositeView is a button-like widget, not a ViewGroup! Don't change my orientation");
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View.inflate(getContext(), R.layout.merge_my_composite_view, this);
        primaryTextView = (TextView) findViewById(R.id.my_composite_text_primary);
        secondaryTextView = (TextView) findViewById(R.id.my_composite_text_secondary);
    }

    public void setPrimaryText(String text) {
        if (valid(text)) {
            primaryTextView.setVisibility(VISIBLE);
        } else {
            primaryTextView.setVisibility(GONE);
        }
        primaryTextView.setText(text);
    }

    private static boolean valid(String text) {
        return text != null && !text.trim().isEmpty();
    }

    public void setSecondaryText(String text) {
        if (valid(text)) {
            secondaryTextView.setVisibility(VISIBLE);
        } else {
            secondaryTextView.setVisibility(GONE);
        }
        secondaryTextView.setText(text);
    }

}

合并布局:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:id="@+id/my_composite_text_primary"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center" />

  <TextView
    android:id="@+id/my_composite_text_secondary"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center" />

</merge>

您可以将其用作:

<?xml version="1.0" encoding="utf-8"?>
<com.example.MyCompositeView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />