【问题标题】:How to get height of default cursor drawable?如何获取默认光标可绘制的高度?
【发布时间】:2016-08-15 15:48:04
【问题描述】:

我知道可以通过将自定义可绘制对象设置为android:textCursorDrawable 来更改光标外观,但有没有办法获得默认可绘制光标的高度?

【问题讨论】:

  • 从光标高度想达到什么效果?
  • @Smit 我正在尝试在 edittext 上放置一个透明视图,该视图具有 edittext 的宽度但光标的高度。
  • @Smit 感谢您的链接。它确实说改变光标的高度。但我希望得到 android 设置的光标的默认高度。

标签: android android-edittext


【解决方案1】:

例如在代码中引用您的 textCursorDrawable:

Drawable cursor = getResources().getDrawable(android.R.drawable.your_drawable);

然后,一旦你有了你的drawable,就使用Drawable Wrapper来包装你的drawable,并使用适当的方法来获取高度: 像这样:

DrawableWrapper with_height_cursor = new DrawableWrapper(cursor);
int height = with_height_cursor.getIntrinsicHeight();

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    这是未经测试的。但这应该可行。

    public int getHeightofCursor(TextView textView) {
        Paint.FontMetricsInt fm = textView.getPaint().getFontMetricsInt();
        int cursorHeight = fm.bottom - fm.top;
        return cursorHeight;
    }
    

    【讨论】:

      【解决方案3】:

      要更改 EditText Cursor 的默认高度,我们需要创建一个带有 shape 标签的可绘制 xml 文件。

      创建editext_cursor.xml并将其放在res->drawable目录下。

      1. 将形状设置为矩形。
      2. 使用size标签我们可以设置光标的宽度。
      3. 使用solid标签我们可以设置EditText Curosor的颜色。

      要调整光标的高度,我们需要使用填充标签,通过属性 android:bottom 和 android:top 我们可以修剪或增加光标高度。

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
          <size android:width="1dip" />
          <solid android:color="#010101" />
          <padding
              android:bottom="-11sp"
              android:top="-2sp" />
          <stroke android:color="#e71839" />
      </shape>
      

      现在,在您的布局文件中,将 android:textCursorDrawable="@drawable/editext_cursor" 添加到 activity.xml 文件中的 EditText 标记。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ebm="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#f2f2f2"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity">
          <EditText
              android:id="@+id/view"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:scrollbars="vertical"
              android:textCursorDrawable="@drawable/editext_cursor"
              android:textSize="20sp">
          </EditText>
      </RelativeLayout> 
      

      【讨论】:

        【解决方案4】:

        使用反射我可以找到光标的高度。它适用于三星 edge s7。如果制造商未对 mCursorDrawableRes 进行任何更改,它可能适用于所有设备。

        public int getHeightofCursor(Context context, EditText editText) throws Exception {
            Field cursor = TextView.class.getDeclaredField("mCursorDrawableRes");
            cursor.setAccessible(true);
            int resid = cursor.getInt(editText);
            Drawable cursorDrawable = context.getResources().getDrawable(resid);
            return cursorDrawable.getIntrinsicHeight();
        }
        

        【讨论】:

        猜你喜欢
        • 2011-03-14
        • 2015-04-15
        • 1970-01-01
        • 2011-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多