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

时间:2013-08-01 06:51:54

标签: android android-edittext italic

以下是显示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" />

在Activity文件中,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"

但问题是Text没有以Italic风格显示,可能是什么问题?请帮帮我。

编辑:它也没有以粗体显示文本。 xml预览它显示正常,但当我在真实设备三星S7562中运行代码时,它只显示正常文本

4 个答案:

答案 0 :(得分:1)

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

edtStartPoint.setHint( R.string.StartingPointHint );

String.xml

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

答案 1 :(得分:0)

只需输入EditText xml属性:

android:textStyle="italic"

答案 2 :(得分:0)

取决于您所看到的内容,但也有一些设备(特别是来自三星)没有正确显示斜体文字。请参阅此处了解一些解决方法:Samsung devices supporting setTypeface(Typeface.Italic)?

答案 3 :(得分:0)

修改

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)