android - 在Android布局xml文件中将TextView的首字母大写

标签 android textview capitalize

我在这样的布局 xml 文件中有一个 TextView:

<TextView
   android:id="@+id/viewId"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/string_id" />

我的字符串是这样指定的:

<string name="string_id">text</string>

是否可以让它显示“文本”而不​​是“文本”无需 java 代码
(并且不改变字符串本身)

最佳答案

没有。但是您可以创建一个简单的 CustomView 扩展 TextView,它覆盖 setText 并将第一个字母大写,正如 Ahmad 所说的那样,并在您的 XML 布局中使用它。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class CapitalizedTextView extends TextView {

    public CapitalizedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (text.length() > 0) {
            text = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
        }
        super.setText(text, type);
    }
}

关于android - 在Android布局xml文件中将TextView的首字母大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624273/

相关文章:

android - 如何防止服务因为未捕获的异常而被 Android 杀死?

android - 从 onDraw() 绘制到创建的位图

java - 如何在 Android 中为按钮文本使用两种不同的字体大小?

Oracle sql 大写土耳其字符

android - 如何使用 Android Developer Console 崩溃报告查找崩溃源?

android - 如何在 Android 中使用 RxJava 从多个来源获取消息

android - 在获取绘图缓存之前从 textview 中删除下划线

Android:将 "internal"链接添加到 TextView 的一部分,链接到我的代码中的操作

python - 关于 Python 首词