android - EditText 斜体样式剪切第一个字母

标签 android android-layout android-edittext android-fonts

我有一个简单的表单,其中有两个带有斜体样式字体的 editText,但是当放置在第一个位置时,一些字母(p 和 j)在左侧被“剪切”。我尝试用drawablePadding修复它,但它不起作用。就我而言,在第一个字母之前插入空格不是解决方案,至少不是最好的解决方案,因为第二个表单字段是密码字段,因此由于空格字符,会自动向用户显示一个点。 EditText 有以下代码:

<EditText
        android:id="@+id/edit_txt_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rectangle_white"
        android:drawableLeft="@drawable/ic_mail"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/login_email_placeholder"
        android:imeOptions="actionNext"
        android:inputType="textEmailAddress"
        android:padding="10dp"
        android:textColor="@color/black"
        android:textCursorDrawable="@null"
        android:textColorHint="@color/color_form_login_text"
        android:textSize="17sp" >

Activity 中:

Typeface edit_text_font = Typeface.createFromAsset(getActivity().getAssets(),
          "fonts/helvetica-bold-italic.ttf");

     //Set Fonts
     mEditTxtLogin.setTypeface(edit_text_font);

错误图片:

screenshot of the bug

解决办法是什么?

最佳答案

这就是解决方案。我使用自定义字体创建了一个自定义 EditText。

package com.example.customcontrols;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

public class MyEditTextItalic extends EditText {
    /*
     * Caches typefaces based on their file path and name, so that they don't
     * have to be created every time when they are referenced.
     */
    private static Typeface mTypeface;

    public MyEditTextItalic(Context context) {
        super(context);
        setTypeface(context);
    }

    public MyEditTextItalic(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(context);
    }

    public MyEditTextItalic(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(context);
    }

    // intercept Typeface change and set it with our custom font
    public void setTypeface(Context context) {
        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getAssets(),
                    "fonts/HelveticaNeue-Bold.otf");
        }

        super.setTypeface(mTypeface,Typeface.ITALIC);

    }
}

非常重要的是不要创建斜体字体,因为无法正确显示,设置字体时使用斜体使用 super.setTypeface(mTypeface,Typeface.ITALIC);

在 .xml 中

<com.example.customcontrols.MyEditTextItalic
            android:id="@+id/edit_txt_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:background="@drawable/rectangle_white"
            android:drawableLeft="@drawable/ic_key"
            android:drawablePadding="10dp"
            android:ems="10"
            android:hint="@string/login_password_placeholder"
            android:imeOptions="actionDone"
            android:inputType="textPassword"
            android:padding="10dp"
            android:textColor="@color/black"
            android:textCursorDrawable="@null"
            android:textStyle="bold|italic"
            android:textColorHint="@color/color_form_login_text"
            android:textSize="17sp" />

关于android - EditText 斜体样式剪切第一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22713430/

相关文章:

Android如何模拟点击任何应用程序

android - 为什么Android模拟器中的相机预览会旋转90度?

android - 如何获取Paytm Merchant id、customer id、channel id和industry type id?

适用于应用程序的基于 Android Tile 的 UI

android - 在 EditText 上禁用键盘

android - 适用于 android 的 Visual Studio (2015) 模拟器无法正常工作 - XDE.exe - 退出代码 3

android - 高程阴影不会仅在预览时显示在设备上

android-layout - TextView Marquee 在 Android 4.0 (ISO) 小部件中不起作用

java - 将 edittext 的值获取到对象中

java - 使用 Spans 获取 EditText 的左侧部分文本