从支持库迁移到 AndroidX 后,某些样式属性丢失
Some style attributes are missing after migrating from support library to AndroidX
参考https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/simple_spinner_item.xml and https://pep-security.lu/gitlab/android/pep/blame/2f5b1397ba73f78f49f2094b9fb370d2fee62635/k9mail/src/main/res/layout/simple_spinner_item.xml
目前,我们有以下下拉视图项。它正在使用样式 ?android:attr/spinnerDropDownItemStyle
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checked_text_view_0"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="48dp"
android:minHeight="48dp" />
</LinearLayout>
我们在自定义ArrayAdapter
中使用上面的XML。然后自定义 ArrayAdapter
将附加到 Spinner
repeatInfoSpinner.setAdapter(repeatInfoArrayAdapter);
当我们使用支持库时(在迁移到 AndroidX 之前),它看起来像这样。它带有漂亮的触摸波纹效果。
迁移到 AndroidX 后,它看起来像传统的全息设计。
看来,以前在支持库 (https://chromium.googlesource.com/android_tools/+/bf45c76e0eb23b7b7a9d5f26b28c16983daa173b/sdk/extras/android/support/v7/appcompat/res/values/themes.xml#33) 中找到的样式属性,在 AndroidX 中不再找到。
我可以知道,我们如何解决这个问题,使我们的应用程序看起来像 material 设计应用程序?
请注意,我已经尝试过 ?attr/spinnerDropDownItemStyle
。没区别。
我之前遇到过一些类似的问题,原来你必须改变你引用这些样式的方式,它们现在在 AndroidX 中更多 "Object-oriented"。在你的情况下,我想你会使用类似的东西:
style="@style/Widget.AppCompat.DropDownItem.Spinner"
代替原来的:
?android:attr/spinnerDropDownItemStyle
如果没有解决,记得检查你的gradle文件,看看是否有androidx的appcompat依赖,应该是这样的:
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
我在 androidx 中尝试简单的演示
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner"
android:padding="8dp"
android:singleLine="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fontFamily="sans-serif"/>
代码端简单的 arrayAdapter 给我正确的结果
class MainActivity : AppCompatActivity() {
val list = listOf<String>("das","fsdfs","fsdfsd","fsdfsd")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner.adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,list)
}
}
还是有问题,请检查对话框和Activity样式主题。
请看下面我的结果
参考https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/simple_spinner_item.xml and https://pep-security.lu/gitlab/android/pep/blame/2f5b1397ba73f78f49f2094b9fb370d2fee62635/k9mail/src/main/res/layout/simple_spinner_item.xml
目前,我们有以下下拉视图项。它正在使用样式 ?android:attr/spinnerDropDownItemStyle
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checked_text_view_0"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="48dp"
android:minHeight="48dp" />
</LinearLayout>
我们在自定义ArrayAdapter
中使用上面的XML。然后自定义 ArrayAdapter
将附加到 Spinner
repeatInfoSpinner.setAdapter(repeatInfoArrayAdapter);
当我们使用支持库时(在迁移到 AndroidX 之前),它看起来像这样。它带有漂亮的触摸波纹效果。
迁移到 AndroidX 后,它看起来像传统的全息设计。
看来,以前在支持库 (https://chromium.googlesource.com/android_tools/+/bf45c76e0eb23b7b7a9d5f26b28c16983daa173b/sdk/extras/android/support/v7/appcompat/res/values/themes.xml#33) 中找到的样式属性,在 AndroidX 中不再找到。
我可以知道,我们如何解决这个问题,使我们的应用程序看起来像 material 设计应用程序?
请注意,我已经尝试过 ?attr/spinnerDropDownItemStyle
。没区别。
我之前遇到过一些类似的问题,原来你必须改变你引用这些样式的方式,它们现在在 AndroidX 中更多 "Object-oriented"。在你的情况下,我想你会使用类似的东西:
style="@style/Widget.AppCompat.DropDownItem.Spinner"
代替原来的:
?android:attr/spinnerDropDownItemStyle
如果没有解决,记得检查你的gradle文件,看看是否有androidx的appcompat依赖,应该是这样的:
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
我在 androidx 中尝试简单的演示
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner"
android:padding="8dp"
android:singleLine="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fontFamily="sans-serif"/>
代码端简单的 arrayAdapter 给我正确的结果
class MainActivity : AppCompatActivity() {
val list = listOf<String>("das","fsdfs","fsdfsd","fsdfsd")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
spinner.adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,list)
}
}
还是有问题,请检查对话框和Activity样式主题。
请看下面我的结果