无法解析 Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu

Cant resolve Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu

我试图创建一个 AutoCompleteTextView,包围一个 TextInputLayout。根据documentation,我应该使用这种风格:

Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu 

但是我无法解析这种风格。 Android Studio 说我只能使用:

@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense
@style/Widget.MaterialComponents.TextInputLayout.FilledBox
@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense
@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox

我的项目使用

implementation 'com.android.support:design:28.0.0'

最终结果应该是这样的:

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <AutoCompleteTextView
      android:id="@+id/filled_exposed_dropdown"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

我在 Android Material 组件的 Github 问题中提出了这个问题。

基于this response,public 文档不是基于当前的稳定版本,而是基于最新的 alpha(或 beta)版本。从 material.io 上的文档无法判断哪些组件处于库的稳定版、alpha 版或 beta 版中。

澄清后,这个问题的解决方案是按照@jeel-vankhede 的建议将您的support-design 库升级到:

implementation 'com.google.android.material:material:1.1.0-alpha07'

相关提示,以防您还使用 ViewModels(lib v.2.0.0):

升级到 Material 设计库的 1.1.0-alpha 版会自动将 androidx.lifecycle 库更新到 2.1.0+。此更新与 2.0.0 相比有重大变化,可能会破坏您的视图模型代码。此更改仅在应用的缩小版本中观察到。

并且不要忘记在您的应用程序中使用 Theme.MaterialComponents

AndroidManifest.xml

<application
    android:theme="@style/AppTheme"
    ...

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

您现在可以使用

implementation 'com.google.android.material:material:1.2.0-alpha05'

根据this issue,文档中的*表示使用TextInputLayout中的任何样式,例如FilledBoxOutlinedBox。 此外,它在 1.1.0 版本中尚不可用,因此我使用此依赖项成功使其工作:

implementation 'com.google.android.material:material:1.2.0-alpha06'

和这两种样式中的任何一种(记得在你的应用程序中使用 Theme.MaterialComponents,正如@R3mx 提到的):

style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"