如何更改Android中的文本/字体设置TextView
?例如,如何使文本变粗?
有没有办法以编程方式设置textStyle
属性TextView
?似乎没有一种setTextStyle()
方法.
要清楚,我不是在谈论View/Widget样式!我在谈论以下内容:
<TextView
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold" />
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Android中的XML文件定义GUI布局.据我所知,没有办法指定您的小部件应该在XML文件中使用自定义字体(例如,您放置在assets/font /中的字体),并且您只能使用系统安装的字体.
我知道,在Java代码中,我可以使用唯一ID手动更改每个窗口小部件的字体.或者,我可以迭代Java中的所有小部件来进行此更改,但这可能会非常慢.
我还有其他选择吗?有没有更好的方法来制作具有自定义外观的小部件?我不是特别想要为我添加的每个新小部件手动更改字体.
这是XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/modal_list_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="7.5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/alert_title_small"
android:textColor="@color/alert_dialog_text_color"
android:textSize="@dimen/alert_dialog_title_font_size"
android:textStyle="bold" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
尽管图形布局显示粗体文本,但设备上却没有.它是设备还是什么?
更新:
对于那些不断要求完整XML的人来说,这里会更新.一个简单的相对布局.这是Java代码:
this.titleTextView.setText(this.modalListState.title);
Run Code Online (Sandbox Code Playgroud)
需要考虑的事项:可能是因为设备字体?有没有人有Galaxy S3确认?这可能是活动风格吗?这是用于整个应用程序的一个:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style …
Run Code Online (Sandbox Code Playgroud) 我有针对组的ExpandableListView的监听器,它们已折叠并展开:
// There is a text view on group layout, no problem there.
@Override
public void onGroupCollapse(int groupPosition) {
// this callback is triggered, however, once the textView is BOLD by onGroupExpanded, the textView is not set back to normal, seems this line of code does nothing...WHY?
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
}
@Override
public void onGroupExpand(int groupPosition) {
// it works fine
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
}
Run Code Online (Sandbox Code Playgroud)
正如你可以在上面看到,我有一textView
组布局,当组展开,我大胆的textView
,如果垮了,我尝试将其设置回取消粗体的Typeface.NORMAL
。
这两个回调被正确触发,但是,一旦textView
为 …
我在我的应用程序中使用textview.我需要以编程方式更改textview的字体.在按钮单击功能中,我需要更改textview字体.如果字体是正常意味着我需要转换为粗体(反之亦然).如果有人知道答案意味着请与我分享.谢谢.
例如:
TextView textView = new TextView(PhotoActivity.this);
textView.setText("Photo not found.");
Run Code Online (Sandbox Code Playgroud)
如何设置风格?
textView.set... ???
Run Code Online (Sandbox Code Playgroud)