转到设置片段后,ImageView的透明背景变黑了

时间:2019-05-13 06:12:07

标签: android android-layout kotlin graphics imageview

我有一个透明背景的ImageView。每次我进入应用程序的设置屏幕时,背景都会变成黑色。从最近的应用程序列表中删除该应用程序后,视图的背景再次变为透明。

视图包含在我的布局中,如下所示:

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp" app:srcCompat="@drawable/soul"
        android:id="@+id/soul" tools:ignore="ContentDescription"
        android:layout_marginEnd="8dp" android:translationZ="7dp"
        app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.45"/>

设置片段如下:

class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.settings, rootKey)
}
}

settings.xml仅具有一些这样的ListPreferences:

<androidx.preference.PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    app:key="rootkey">
    <ListPreference
        app:key="levellength"
        app:title="@string/setlevellength"
        app:summary="@string/setlevellengthdescription"
        app:defaultValue="42"
        app:entries="@array/levellengthkey"
        app:entryValues="@array/levellength"/>
</androidx.preference.PreferenceScreen>

我只有一个活动。这些设置包含在onResume中,如下所示:val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

对于那些感兴趣的人:视图位于此文本的正下方。它是白色和透明的混合,因此,除非您有深色背景,否则必须将其拖动才能看到它。 The View. Drag it.

1 个答案:

答案 0 :(得分:0)

我自己找到答案的方法是,仅在xml中设置imageview的背景,而不是仅仅从png中获取背景。

<ImageView
    android:layout_width="60dp" android:background="@drawable/soul"
    android:layout_height="60dp" app:srcCompat="@drawable/soul"
    android:id="@+id/soul" tools:ignore="ContentDescription"
    android:layout_marginEnd="8dp" android:translationZ="7dp"
    app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.45"/>