我在布局 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)