gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:22 29 4
gpt4 key购买 nike

我在这样的布局 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com