繁体   English   中英

Android kotlin - 黑色透明背景的活动和白色背景的 SearchView

[英]Android kotlin - Activity with black transparent background and SearchView with white background

正如标题所说,我创建了一个带有黑色透明背景的 Activity,在它上面有一个带有白色背景和灰色/自定义文本/图标颜色的 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>

我怎样才能做到这一点? 几个小时以来,我已经在寻找解决这种小事的方法了! 安卓真是太可怕了!

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

主要问题是 Translucent 样式中的这一行<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>

结果是这样的

最后结果

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM