android - View 中的灰色背景颜色虽然定义了白色背景并首先工作

标签 android

我有一个奇怪的问题。我的应用程序在我的 Samsung Galaxy II S 和 Android 2.3.4 上完美运行。在我的 Android 3.0 平板电脑上有一个奇怪的行为:

明确设置为白色的背景颜色突然变成了不同的灰色。

  • 我有两个 Activity 受到影响,一个带有稍微复杂一点的 WebView,另一个非常简单,只有 4 个按钮。它们的 LinearLayout、ScrollView、TextView 等都有明确的白色背景。
  • 应用启动,一切正常,背景为白色
  • 我在复杂的 Activity 中来回切换,一段时间后背景不再是白色,而是不同的灰色调。在那之后,它们将保持灰色,对于使用 4 个按钮的简单 Activity 也是如此
  • 我不改变代码中的背景颜色

以下几点可能导致此行为:

  • 使用 WebView 。它并非一直都在使用,而且似乎这种行为发生得更快/仅当 WebView 可见时
  • 改变方向似乎会插入这种行为

到目前为止我尝试了什么:

  • 禁用硬件加速
  • 强制重绘
  • 尝试重新排列复杂 Activity 中的 View ,使它们不重叠以消除警告“无法在第一个布局后获取 viewWidth”

对我来说,这似乎是一种可视化 Debug模式,但我还没有在网络上找到任何关于这种模式的信息。

可能是唯一的提示: 我收到一条警告:无法在第一个布局后获取 viewWidth

简单 Activity 的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >

<Button
    android:id="@+id/ag_btn_pressespiegel"
    android:text="@string/artikelgruppen_pressespiegelbutton"
    android:layout_width="fill_parent"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:textColor="@color/black"
    android:layout_marginBottom="16dp" />

<Button
    android:id="@+id/ag_btn_dossier"
    android:text="@string/artikelgruppen_dossierbutton"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:layout_marginBottom="16dp" />

<Button
    android:id="@+id/ag_btn_tag"
    android:text="@string/artikelgruppen_tagbutton"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:layout_marginBottom="32dp" />

<Button
    android:id="@+id/ag_btn_sync"
    android:text="@string/artikelgruppen_syncbutton"
    android:layout_marginLeft="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:layout_marginBottom="16dp" />

</LinearLayout>

WebView 的完整 Activity 代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ad_sv_artikeldetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >

<RelativeLayout
    android:id="@+id/ad_rl_artikeldetails"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="@color/white" >

        <ImageView
            android:id="@+id/ad_img_kanal"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp" >
        </ImageView>

        <ImageView
            android:id="@+id/ad_img_pdf"
            android:layout_width="24dp"
            android:layout_height="16dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="6dp"
            android:layout_marginTop="54dp"
            android:src="@drawable/pdfdocument" >
        </ImageView>

        <ImageView
            android:id="@+id/ad_img_land"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="4dp"
            android:layout_marginTop="5dp" >
        </ImageView>

        <TextView
            android:id="@+id/ad_txt_medie"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="43dp"
            android:layout_marginRight="120dp"
            android:layout_marginTop="22dp"
            android:maxLines="1"
            android:background="@color/white"
            android:textColor="@color/black" >
        </TextView>

        <TextView
            android:id="@+id/ad_txt_datum"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="6dp"
            android:background="@color/white"
            android:layout_marginTop="22dp" >
        </TextView>
    </RelativeLayout>

    <!-- Titel & Grosses Linarlayout für vertikale Anordnung -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:background="@color/white"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/ad_txt_titel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="35dp"
            android:layout_marginTop="0dp"
            android:background="@color/white"
            android:maxLines="2"
            android:textColor="@color/black"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/ad_txt_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:textColor="@color/black" >
        </TextView>

        <WebView
            android:id="@+id/ad_web_html"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:clickable="false" >
        </WebView>

        <!-- Thema -->

        <LinearLayout
            android:id="@+id/ad_ll_thema"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_thema_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:background="@color/white"
                android:text="@string/artikeldetail_thema"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_thema"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <!-- Suchbegriff -->

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_suchbegriff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_suchbegriff_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:background="@color/white"
                android:text="@string/artikeldetail_suchbegriff"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_suchbegriff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:layout_marginTop="0dp"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <!-- Liefer & Ausgabedatum -->

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_lieferdatum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_lieferung"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:background="@color/white"
                android:text="@string/artikeldetail_lieferdatumlieferung"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_lieferung_datum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:background="@color/white"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_ausgabedatum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_ausgabe"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:background="@color/white"
                android:layout_marginTop="0dp"
                android:text="@string/artikeldetail_ausgabedatum"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_ausgabe_datum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_medienart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_lieferung"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@color/white"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:text="@string/artikeldetail_medienart"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_medienart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_medientyp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_ausgabe"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@color/white"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:text="@string/artikeldetail_medietyp"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_medietyp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ad_ll_kategorien"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/ad_txt_kategorien_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@color/white"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="0dp"
                android:text="@string/artikeldetail_kategorien"
                android:textColor="@color/black" >
            </TextView>

            <TextView
                android:id="@+id/ad_txt_kategorien"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="12dp"
                android:textColor="@color/black" >
            </TextView>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
</ScrollView>

将复杂 Activity 的屏幕截图与不正确的灰色背景(左)、正确的白色背景(右站点)相结合的链接:http://int.argus.ch/screenshots.jpg

演示改变方向导致问题的视频链接(在另一个 Activity 中)(相当大 50MB):http://int.argus.ch/grey.mp4

由于我是新手,这里无法贴出简单 Activity 截图的链接,但是LinearLayout的背景色不再是白色,而是灰色。

PS:我将@white 定义更改为蓝色。一切都是蓝色的。如果出现问题,一切也是灰色的,而不是不同的蓝色!

提前致谢

最佳答案

你能尝试将它用于纯色(这里是白色)背景吗

android:cacheColorHint="#ffffffff"

如果您使用可绘制对象作为背景,请尝试使用此

android:cacheColorHint="#00000000"

让我知道这是否有效

关于android - View 中的灰色背景颜色虽然定义了白色背景并首先工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497358/

相关文章:

java - 指南针应用程序无法实例化 Activity

Android - 如何获取应用程序名称? (不是包名)

android - Paypal 支付: How to get success request when loading the paypal in webview

android - Kotlin Android 启动新 Activity

android - Marshmallow 及以上版本的 Wifi 连接接收器

android线性布局将两个项目并排放置

android - 对话框中的微调器 - NullPointerException - Android

java - 如何从 Android 中的应用程序访问我的相机

Android:锁屏时正在播放背景音乐

android - 如何使用用户设备上的帐户创建 Android 应用程序?