让EditText的提示文字变为斜体?

3

你知道如何在xml中实现这个功能吗?

我在一个相关的问题中看到了java代码来实现它,检查长度是否为0:

EditText.setText(Html.fromHtml("<small><i>" + "Text Hint Here" + "</i></small>"));

你能仅从xml中做这件事吗?
编辑:
<EditText
        android:id="@+id/term_entry"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/scrllyout"
        android:imeOptions="actionNone|flagNoExtractUi"
        android:inputType="text|textImeMultiLine"
        android:singleLine="true"
        android:hint="Enter Command"
        android:textStyle="italic"
        android:textColorHint="#30AAAAAA"
        />

1
是的,你可以从XML中设置 android:textstyle="italic" 来实现。 - Rahul Baradia
2个回答

0

在XML中尝试像这样

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    android:hint="hint" 
    android:textStyle="italic" > 
</EditText>

要制作提示,请使用android:hint="提示",要使斜体文字,请像这样使用android:textStyle="italic"

更新:

这是XML代码,请尝试一下,我已在我的笔记本电脑上测试过了,可以正常工作。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >


<EditText
    android:id="@+id/term_entry"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/scrllyout"
    android:hint="Enter Command"
    android:imeOptions="actionNone|flagNoExtractUi"
    android:inputType="text|textImeMultiLine"
    android:singleLine="true"
    android:textColorHint="#ffffff"
    android:textStyle="italic" />

</LinearLayout>

我尝试了这个,但是没有任何作用。我会给你展示我的代码。我会在上面进行编辑。 - Paul
仍然不是斜体,使用相同的代码放在相对布局中。奇怪。 - Paul

0
最好使用Java代码。使用Typeface。将字体文件放在asset文件夹中。然后编写以下代码。
italic = Typeface.createFromAsset(getAssets(), "Your font file"); 
editText.setTypeface(italic);  

为什么更好?如果你在代码中设置字体,可能会出现重复设置的情况。从 XML 中进行操作通常是更好和更清晰的解决方案。 - Simon Schubert
是的,我更喜欢从XML中进行操作,但好像没有相应的命令? - Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接