本文共 4077 字,大约阅读时间需要 13 分钟。
本文翻译自:
I am having this issue where I am using the Android's Holo theme on a tablet project. 我有这个问题,我在平板电脑项目中使用Android的Holo主题。 However, I have a fragment on screen which has a white background. 但是,我在屏幕上有一个具有白色背景的片段。 I am adding an EditText
component on this fragment. 我在这个片段上添加了一个EditText
组件。 I've tried to override the theme by setting the background of the Holo.Light theme resources. 我试图通过设置Holo.Light主题资源的背景来覆盖主题。 However, my text cursor (carat) remains white and hence, invisible on screen (I can spot it faintly in the edittext field..). 但是,我的文本光标(克拉)仍然是白色的,因此在屏幕上看不见(我可以在编辑文本字段中隐约发现它...)。
Does anyone know how I can get EditText to use a darker cursor color? 有谁知道如何让EditText使用更暗的光标颜色? I've tried setting the style of the EditText to "@android:style/Widget.Holo.Light.EditText"
with no positive result. 我已经尝试将EditText的样式设置为"@android:style/Widget.Holo.Light.EditText"
,没有任何正面结果。
参考:
It appears as if all the answers go around the bushes. 似乎所有的答案都围绕着灌木丛。
In your EditText
, use the property: 在EditText
,使用属性:
android:textCursorDrawable="@drawable/black_cursor"
and add the drawable black_cursor.xml
to your resources, as follows: 并将drawable black_cursor.xml
添加到您的资源,如下所示:
This is also the way to create more diverse cursors, if you need. 如果需要,这也是创建更多样化游标的方法。
In Layout 在布局中
Then create drawable xml: color_cursor 然后创建drawable xml:color_cursor
You have a white color cursor on EditText property. EditText属性上有一个白色光标。
Its even easier than that. 它比这更容易。
This works in ICS and beyond. 这适用于ICS及更高版本。 I haven't tested it in other versions. 我没有在其他版本中测试它。
There is a new way to change cursor color in latest Appcompact
v21 在最新的Appcompact
v21中有一种更改光标颜色的新方法
colorAccent
in style like this: 只需更改colorAccent
样式,如下所示: Then apply this style on your app theme or activities. 然后在您的应用主题或活动上应用此样式。
Update : this way only works on API 21+ 更新 :这种方式仅适用于API 21+
2.3.7 - didn't work4.4.4 - worked5.0 - worked5.1 - worked
For anyone that needs to set the EditText
cursor color dynamically, below you will find two ways to achieve this. 对于需要动态设置EditText
光标颜色的任何人,下面你会发现两种方法来实现这一点。
First, create your cursor drawable: 首先,创建你的光标drawable:
Set the cursor drawable resource id to the drawable you created (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)): 将光标drawable资源id设置为您创建的drawable(https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)) :
try { Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); f.setAccessible(true); f.set(yourEditText, R.drawable.cursor);} catch (Exception ignored) {}
To just change the color of the default cursor drawable, you can use the following method: 要仅更改默认光标drawable的颜色,可以使用以下方法:
public static void setCursorDrawableColor(EditText editText, int color) { try { Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); fCursorDrawableRes.setAccessible(true); int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); Field fEditor = TextView.class.getDeclaredField("mEditor"); fEditor.setAccessible(true); Object editor = fEditor.get(editText); Class clazz = editor.getClass(); Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); fCursorDrawable.setAccessible(true); Drawable[] drawables = new Drawable[2]; Resources res = editText.getContext().getResources(); drawables[0] = res.getDrawable(mCursorDrawableRes); drawables[1] = res.getDrawable(mCursorDrawableRes); drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); fCursorDrawable.set(editor, drawables); } catch (final Throwable ignored) { }}
转载地址:http://dpcnb.baihongyu.com/