如何TextView在Java中设置样式(粗体斜体)而不使用XML布局?
换句话说,我需要android:textStyle用Java 编写.
所以我想android:fontFamily在Android中更改,但我没有在Android中看到任何预定义的字体.如何选择其中一个预定义的?我真的不需要定义自己的TypeFace,但我所需要的只是它现在所显示的内容.
<TextView
android:id="@+id/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:gravity="center"
android:text="CallerBlocker"
android:textSize="40dp"
android:fontFamily="Arial"
/>
Run Code Online (Sandbox Code Playgroud)
看来我在那里做的不会真的有效!BTW android:fontFamily="Arial"是一次愚蠢的尝试!
如何更改a中的字体TextView,默认显示为Arial?如何将其更改为Helvetica?
如何(如果可能的话)我可以在我的资产文件夹中使用字体在ActionBar标题文本中设置自定义字体(仅限 - 而不是标签文本)?我不想使用android:logo选项.
很多时候,我们需要将TextView的字体自动调整到给定的边界.
可悲的是,即使有很多线程和帖子(和建议的解决方案)谈论这个问题(例如这里,这里和这里的例子),它们都没有真正运作良好.
这就是为什么,我决定测试每一个,直到找到真正的交易.
我认为来自这样一个textView的要求应该是:
应允许使用任何字体,字体,样式和字符集.
应该处理宽度和高度
没有截断,除非文本由于限制而无法适应,我们已经给它(例如:文本太长,可用大小太小).但是,如果我们愿意,我们可以请求水平/垂直滚动条,仅适用于那些情况.
应该允许多线或单线.如果是多行,请允许最大和最小行.
计算速度不应该慢.使用循环找到最佳尺寸?至少要优化它,不要每次增加1个采样.
在多行的情况下,应该允许更喜欢调整大小或使用更多行,和/或允许使用"\n"字符自己选择行.
我已经尝试了很多样本(包括链接的那些,我已经写过了),而且我也尝试修改它们来处理这些情况,我已经谈过了,但没有一个真正起作用.
我做了一个示例项目,让我可以直观地看到TextView是否正确自动匹配.
目前,我的示例项目只是随机化文本(英文字母加上数字)和textView的大小,并让它保持单行,但即使这样也不适用于我尝试的任何样本.
这是代码(也可在此处获得):
res/layout/activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:text="Button" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_above="@+id/button1"
android:layout_alignParentLeft="true" android:background="#ffff0000"
android:layout_alignParentRight="true" android:id="@+id/container"
android:layout_alignParentTop="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
src/.../MainActivity.javapublic class MainActivity extends Activity
{
private final Random _random =new Random();
private static final String ALLOWED_CHARACTERS ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); …Run Code Online (Sandbox Code Playgroud) 我已将自定义字体文件添加到我的assets/fonts文件夹中.如何从我的XML中使用它?
我可以从代码中使用它如下:
TextView text = (TextView) findViewById(R.id.textview03);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
text.setTypeface(tf);
Run Code Online (Sandbox Code Playgroud)
我不能使用android:typeface="/fonts/Molot.otf"属性从XML做到吗?
我的问题很简单:
在我的每个textview中,我目前正在使用该属性
android:fontFamily="sans-serif-light"
Run Code Online (Sandbox Code Playgroud)
在后HC设备上提供华丽的外观.
不幸的是,这不适用于每个小部件,对于我的Spinners,我需要覆盖适配器.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//You can use the new tf here.
if(convertView == null || convertView.getTag() == null) {
// new view - populate
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
convertView.setTag(new Object());
}
CheckedTextView spinner_text=(CheckedTextView) convertView.findViewById(android.R.id.text1);
//Typeface should be set here...
return spinner_text;
}
}
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法通过代码获得完全相同的结果?
PS:不,我不想在资产文件夹中添加字体,我只想使用系统一.
在我的应用程序中,我需要对所有textview和编辑文本字段使用helvetica字体.除了对每个textview使用settypeface方法之外,还有什么方法可以做到这一点吗?任何建议都会有很大的帮助.
提前致谢 !
如何使用我的xml资产文件夹中添加的自定义字体?我知道我们可以setTypeface()在java中使用方法,但我们必须在我们使用它的地方执行此操作TextView.那么还有更好的方法吗?
可能重复:
是否可以为整个应用程序设置字体?
我需要为整个应用程序设置自定义字体(.ttf格式),我该怎么办?如果可能,从Manifest或XML将是最好的选择