从支持库迁移到AndroidX后,缺少某些样式属性

时间:2019-04-10 11:01:04

标签: android androidx

通过引用https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/simple_spinner_item.xmlhttps://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之前),它看起来像这样。它具有不错的触摸波纹效果。

enter image description here


迁移到AndroidX后,它看起来就像是传统的全息设计。

enter image description here


似乎,以前在支持库(https://chromium.googlesource.com/android_tools/+/bf45c76e0eb23b7b7a9d5f26b28c16983daa173b/sdk/extras/android/support/v7/appcompat/res/values/themes.xml#33)中找到的style属性在AndroidX中不再存在。


我可以知道,我们如何解决这个问题,使我们的应用看起来像材料设计应用?

注意,我尝试过?attr/spinnerDropDownItemStyle。没关系。

2 个答案:

答案 0 :(得分:1)

我尝试在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)
}
}

如果仍然有问题,请检查对话框和活动样式主题。

请看下面我的结果

enter image description here

答案 1 :(得分:0)

我以前也遇到过类似的问题,事实证明,您必须更改引用这些样式的方式,而在AndroidX中,它们现在更加“面向对象”。对于您的情况,我认为您将使用类似以下内容:

style="@style/Widget.AppCompat.DropDownItem.Spinner"

代替原来的

?android:attr/spinnerDropDownItemStyle

如果无法解决,请记住检查gradle文件,看看是否存在androidx的appcompat依赖项,应该是这样的:

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'