Android Kotlin-具有黑色透明背景的活动和具有白色背景的SearchView

时间:2020-02-24 00:28:37

标签: android

正如标题所述,我使用透明背景为黑色并在其顶部使用白色/灰色/自定义文本/图标颜色的SearchView来创建活动。

这是我到目前为止所拥有的:

activity_search_input.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchInput">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="58dp"
        android:layout_height="60dp"
        android:background="#FFFFFF"
        app:layout_constraintEnd_toStartOf="@+id/searchInput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_back" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <SearchView
        android:id="@+id/searchInput"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

即使背景设置为#FFFFFF,它也是灰色的!

在清单中:

<activity
    android:name=".SearchInput"
    android:theme="@style/Theme.AppCompat.Translucent">
</activity>

在styles.xml中:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#66000000</item> <!-- Or any transparency or color you need -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

我该怎么做?几个小时以来,我一直在寻找解决此类小问题的解决方案! Android绝对可怕!

1 个答案:

答案 0 :(得分:0)

除非我不知道有一种简单的方法,否则进行真正的测试确实是一种可怕的经历。这是我为您找到的解决方法。

主要问题是您的半透明样式的这一行<item name="android:background">#66000000</item>,弄乱了searchView在幕后使用的颜色。删除它,您为searchView设置的背景现在应该可以正常工作。

但是您会注意到所有图标和textcolor仍为白色,您将不得不分别使用自定义图标设置这些图标(由于某些原因,textColor仍然不会改变),或者只是使用parent="Theme.AppCompat.Light.NoActionBar"作为父图标您的半透明主题。

所以现在您的自定义半透明主题应该如下所示:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

现在,由于您已从主题中删除了窗口背景,因此需要使用android:background="#66000000"

为根布局设置背景

所以您的activity_search_input.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66000000"
    tools:context=".SearchInput">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="58dp"
        android:layout_height="60dp"
        android:background="#FFFFFF"
        app:layout_constraintEnd_toStartOf="@+id/searchInput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_back" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <SearchView
        android:id="@+id/searchInput"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果是这样

final result

尝试了不同的方法,但这感觉最简单,请检查是否对您有帮助,否则有人会想出更好的方法。