Android 如何获得默认光标可绘制的高度?
我知道可以通过将自定义可绘制设置为android:textCursorDrawable来更改光标外观,但是有没有办法获得默认光标可绘制的高度?请在代码中引用您的textCursorDrawable,例如:Android 如何获得默认光标可绘制的高度?,android,android-edittext,Android,Android Edittext,我知道可以通过将自定义可绘制设置为android:textCursorDrawable来更改光标外观,但是有没有办法获得默认光标可绘制的高度?请在代码中引用您的textCursorDrawable,例如: Drawable cursor = getResources().getDrawable(android.R.drawable.your_drawable); 然后,一旦您有了可拉伸包装,使用可拉伸包装纸包装您的可拉伸包装,并使用适当的方法获得高度: 像这样: DrawableWrapper
Drawable cursor = getResources().getDrawable(android.R.drawable.your_drawable);
然后,一旦您有了可拉伸包装,使用可拉伸包装纸包装您的可拉伸包装,并使用适当的方法获得高度:
像这样:
DrawableWrapper with_height_cursor = new DrawableWrapper(cursor);
int height = with_height_cursor.getIntrinsicHeight();
希望这有帮助 要更改EditText光标的默认高度,我们需要创建一个带有形状标记的可绘制xml文件 创建editext_cursor.xml并将其置于res->drawable目录下
<?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\u 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>
使用反射,我可以找到光标的高度。它已经为三星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();
}
这是未经测试的。但这应该行得通
public int getHeightofCursor(TextView textView) {
Paint.FontMetricsInt fm = textView.getPaint().getFontMetricsInt();
int cursorHeight = fm.bottom - fm.top;
return cursorHeight;
}
您希望从光标的高度获得什么?@Smit我正在尝试在edittext上放置一个透明视图,该视图的宽度为edittext,但光标的高度为。@Smit感谢您的链接。它确实说明了如何更改光标的高度。但我希望得到android设置的默认光标高度。参考,