【发布时间】:2015-04-06 17:56:53
【问题描述】:
这是我在开发真实应用程序时发现的“错误”,但我创建了一个空白项目来重现它。
我有以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/root"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.example.test.testapp.MyEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MyEditText 类如下所示:
public class MyEditText extends EditText {
public MyEditText(Context context){
super(context);
}
public MyEditText(Context context, AttributeSet attrs){
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
}
我的styles.xml 文件是空的,除了主题
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
我希望MyEditText 看起来像普通的EditText,它在 Android 5.0 上是这样,但在 Android 2.3.7、Android 4.1.3 或 Android 4.4.4 上却不是。
在那些 Android 版本上,EditTexts 的颜色不同,正常的在聚焦时有青色下划线,自定义的有黑色下划线:
为什么会发生这种情况,我该如何预防?
编辑/更新:
Google seems to have adressed this 在支持库中引入 AppCompatEditText 类等。
【问题讨论】:
标签: android android-layout android-edittext android-custom-view