vin*_*3m1 21
我不得不深入研究Android源码以找到答案,但你必须在自定义形状drawable上使用填充.
注意:由于支持,仅适用于API 12及更高版本textCursorDrawable
用积极的 顶部填充,以移动顶光标的更高
用户正面 底部填充移动底部光标的低
我通常最终使用负底部填充来缩短光标,因为当使用lineSpacingMultiplier或增加行高时它会低于基线lineSpacingExtra.
示例cursor_red.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size
android:width="2dip" />
<solid
android:color="@color/red" />
<padding
android:top="2sp"
android:bottom="-11sp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
这将产生一个2dip宽的红色光标
然后在您的edittext中,只需指定android:textCursorDrawable:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_red" />
Run Code Online (Sandbox Code Playgroud)
在Editor.java中相关的Android源代码,我从中找到了解决方案:
private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) {
...
mCursorDrawable[cursorIndex].getPadding(mTempRect);
...
mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width,
bottom + mTempRect.bottom);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
Paddings 可用于控制光标高度
游标文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="2dp" />
<solid android:color="@color/black" />
</shape>
Run Code Online (Sandbox Code Playgroud)
cursor_with_padding.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="7dp"
android:drawable="@drawable/cursor"
android:top="8dp" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)
布局
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/cursor_with_padding"/>
Run Code Online (Sandbox Code Playgroud)
将 editText 样式更改为自定义样式
<item name="android:editTextStyle">@style/myEditText</item>
Run Code Online (Sandbox Code Playgroud)
然后使用可绘制对象来设置光标:
<style name="myEditText" parent="@android:parentdetails">
<item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item>
<item name="android:height">Height here e.g. 50sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10009 次 |
| 最近记录: |