如何在Android Dialer应用程序中的TextView中添加大小字母?

时间:2015-04-21 14:20:51

标签: android android-layout android-button android-dialer

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

抱歉:“nuber”=“号码”

enter image description here

2 个答案:

答案 0 :(得分:2)

看起来您在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" />

答案 1 :(得分:0)

您可以尝试将Html.fromHtml用作:

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

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