【发布时间】:2017-05-05 12:52:19
【问题描述】:
我正在尝试更改 EditText 光标指针颜色(从原色蓝色变为白色),但没有解决方案适用于我的项目。我试图开发一个演示项目,其中相同的代码运行良好。 此代码适用于 >=android 5.0
我不知道,哪个属性被我的值覆盖了。遇到这个问题我有两种选择:
- 找到控制该颜色值的属性。
- 在页面加载时(在 onViewCreated 中)通过 java 代码更改颜色值。
请建议如何解决此问题。
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="my_text_layout_style">
<item name="android:textColor">#FFF</item>
<item name="android:textColorHint">#FFF</item>
<item name="colorAccent">#FFF</item>
<item name="colorControlNormal">#FFF</item>
<item name="colorControlActivated">#FFF</item>
<item name="colorControlHighlight">#FFF</item>
</style>
<style name="my_edit_text_style">
<item name="colorAccent">#FFF</item>
<item name="android:editTextStyle">@style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText" />
</resources>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/base_disable_btn_bg_color"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:hint="just a hint"
app:hintTextAppearance="@style/my_text_layout_style"
android:theme="@style/my_text_layout_style"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:theme="@style/my_edit_text_style"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
试图以编程方式添加视图但没有成功
AppLinearLayout appLinearLayout = (AppLinearLayout) view.findViewById(R.id.some_id);
EditText editText = new EditText(new ContextThemeWrapper(getContext(), R.style.AppTheme_Cursor));
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
appLinearLayout.addView(editText);
【问题讨论】:
-
尝试更改 color.xml 中的 colorAccent 颜色并让我知道它是否有效
-
@JamesMacca 尝试了上面的答案,它只改变光标颜色而不是指针颜色
-
@Nainal 检查我的代码,我试过了。
-
@HarishGyanani 啊,没问题。
-
从最新的 android studio 版本中,style.xml 有两个文件夹,你有没有在这两个文件夹中定义你的风格。一个是style另一个是style-V21
标签: android android-edittext android-textinputedittext