TextInputLayout 和 AutoCompleteTextView 背景在预览中看起来不错,但在设备上它总是白色的
TextInputLayout and AutoCompleteTextView background looks fine in preview, but on device it is always white
尽可能短。
这是我的 TextInputLayout 样式
<style name="TextLabel" parent="Widget.AppCompat.AutoCompleteTextView">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/pure_white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/pure_white</item>
<item name="colorControlNormal">@color/pure_white</item>
<item name="colorControlActivated">@color/pure_white</item>
<!-- When everything else failed I tried this but with no effect -->
<item name="android:background">@android:color/transparent</item>
</style>
这是布局中的实现
<android.support.design.widget.TextInputLayout
android:theme="@style/TextLabel"
android:layout_margin="@dimen/activity_margin_basic_double"
android:layout_centerInParent="true"
android:id="@+id/team_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/new_team_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_team_create"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
这是我在设备上得到的(包括物理设备和模拟设备):
但这是我真正想要的,Android Studio 预览实际上正确显示了它,只是不会以这种方式编译:
有什么办法可以解决这个问题吗?我已经浪费了几天时间试图解决这个问题,但就是无法解决问题
非常感谢!
斯洛巴
问题出在你的风格上。您正在将 Widget.AppCompat.AutoCompleteTextView
应用于 TextInputLayout
,并得到您所看到的结果。您可以将此父级样式更改为 AppTheme
或将 android:theme="@style/TextLabel"
移动到 AutoCompleteTextView
并且不更改父级。我在下面展示第一种方式。
这个小视频展示了我为获得您想要的东西所做的更改。我添加了一些东西以更好地展示它是如何工作的。它可能不能 100% 满足您的需求,但它应该能让您跨过门槛。
Demo video
这里是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFcc9c00"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/team_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:theme="@style/TextLabel">
<AutoCompleteTextView
android:id="@+id/new_team_name"
android:layout_width="match_parent"
android:hint="Choose a Team Name"
android:layout_height="wrap_content"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/team_name_container"
android:layout_margin="8dp"
android:hint="EditText hint" />
</RelativeLayout>
和styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="TextLabel" parent="AppTheme">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@android:color/white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@android:color/white</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="colorControlActivated">@android:color/white</item>
</style>
</resources>
尽可能短。
这是我的 TextInputLayout 样式
<style name="TextLabel" parent="Widget.AppCompat.AutoCompleteTextView">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/pure_white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/pure_white</item>
<item name="colorControlNormal">@color/pure_white</item>
<item name="colorControlActivated">@color/pure_white</item>
<!-- When everything else failed I tried this but with no effect -->
<item name="android:background">@android:color/transparent</item>
</style>
这是布局中的实现
<android.support.design.widget.TextInputLayout
android:theme="@style/TextLabel"
android:layout_margin="@dimen/activity_margin_basic_double"
android:layout_centerInParent="true"
android:id="@+id/team_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/new_team_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_team_create"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
这是我在设备上得到的(包括物理设备和模拟设备):
但这是我真正想要的,Android Studio 预览实际上正确显示了它,只是不会以这种方式编译:
有什么办法可以解决这个问题吗?我已经浪费了几天时间试图解决这个问题,但就是无法解决问题
非常感谢! 斯洛巴
问题出在你的风格上。您正在将 Widget.AppCompat.AutoCompleteTextView
应用于 TextInputLayout
,并得到您所看到的结果。您可以将此父级样式更改为 AppTheme
或将 android:theme="@style/TextLabel"
移动到 AutoCompleteTextView
并且不更改父级。我在下面展示第一种方式。
这个小视频展示了我为获得您想要的东西所做的更改。我添加了一些东西以更好地展示它是如何工作的。它可能不能 100% 满足您的需求,但它应该能让您跨过门槛。
Demo video
这里是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFcc9c00"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/team_name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:theme="@style/TextLabel">
<AutoCompleteTextView
android:id="@+id/new_team_name"
android:layout_width="match_parent"
android:hint="Choose a Team Name"
android:layout_height="wrap_content"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/team_name_container"
android:layout_margin="8dp"
android:hint="EditText hint" />
</RelativeLayout>
和styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="TextLabel" parent="AppTheme">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@android:color/white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@android:color/white</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="colorControlActivated">@android:color/white</item>
</style>
</resources>