【问题标题】:EditText with italic style cut the first letter带有斜体样式的 EditText 剪切了第一个字母
【发布时间】:2014-03-28 12:52:26
【问题描述】:

我有一个简单的表单,其中包含两个带有斜体样式字体的 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" >

活动中:

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

     //Set Fonts
     mEditTxtLogin.setTypeface(edit_text_font);

错误图片:

解决办法是什么?

【问题讨论】:

  • @nomada 你能在下面看到我的回答吗
  • 删除 em 不起作用

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


【解决方案1】:

这就是解决方案。我用自定义字体创建了一个自定义 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" />

【讨论】:

    【解决方案2】:

    尝试向左添加填充。试试这个:

    <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:paddingLeft="45dp"
            android:textColor="@color/black"
            android:textCursorDrawable="@null"
            android:textColorHint="@color/color_form_login_text"
            android:textSize="17sp" >
    

    【讨论】:

      【解决方案3】:

      尝试给予android:paddingLeft 超过10dp 或将android:drawablePadding 减少到 4 或 5dp

      【讨论】:

        【解决方案4】:

        这对我有用,将自定义 width 设置为 xml 布局:

        android:width="130dp" //Define a value larger than your text.
        

        【讨论】:

          【解决方案5】:

          您可以通过给TextView 赋予文本阴影来强制它在其边界之外绘制。如果您将阴影颜色设置为透明,那么您就有了解决方案!将这些添加到您的TextView

          android:shadowColor="#00FFFFFF"
          android:shadowDx="48"
          android:shadowDy="0"
          android:shadowRadius="1"
          

          在我的测试中我发现 #00000000 不起作用,48 的大小足以显示所有剪切的文本,并且 shadowDy 和 shadowRadius 是必要的。

          【讨论】:

            猜你喜欢
            • 2012-12-02
            • 1970-01-01
            • 1970-01-01
            • 2010-12-13
            • 1970-01-01
            • 2016-11-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多