为什么我无法在EditText中设置斜体样式



以下是显示EditText、的代码

 <EditText
     android:id="@+id/editStartingPoint"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:background="@null"
     android:gravity="left|center_vertical"
     android:hint="@string/StartingPointHint"
     android:inputType="text"
     android:maxLength="8"
     android:padding="10dp"
     android:text=""
     android:textColor="@color/login_lbl"
     android:textSize="@dimen/settings_list_font_size" />

在活动文件中,onCreate()方法,

edtStartPoint = (EditText) findViewById(R.id.editStartingPoint);
edtStartPoint.setText ( Html.fromHtml( "<small><i>" + "This should be in Italic" + "</i></small>" ));

我也试过了,

edtStartPoint.setTypeface( null, Typeface.ITALIC );

我还尝试在.xml文件中设置textStyle="italic"

但问题是文本没有以斜体显示,可能是什么问题?请帮帮我。

编辑:它也不会以粗体显示文本。xml预览它显示得很好,但当我在真实设备Samsung S7562中运行代码时,它只显示普通文本

编辑:

android:textstyle添加到xml中应该可以。

如果您使用自定义字体,请确保您的字体具有italic样式。来自setTypeface(Typeface)方法的文档:

Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTypeface(Typeface, int) to get the appearance that you actually want.

如果您的自定义确实支持斜体,请尝试此操作,mTextView.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC)

我通过在结束onCreate()方法的}之前写入以下行来解决问题

edtStartPoint.setHint( R.string.StartingPointHint );

字符串.xml

<string name="StartingPointHint"><i>This is italic</i></string>

只需输入EditText xml属性:

android:textStyle="italic"

这取决于您所看到的内容,但也有一些设备(尤其是三星的设备)不能正确显示斜体文本。请参阅此处了解一些解决方案:三星设备支持setTypeface(TypefaceItalic)?

最新更新