【问题标题】:Grey background colors in views although white background defined and working first视图中的灰色背景颜色,尽管首先定义了白色背景并开始工作
【发布时间】:2012-03-01 15:51:18
【问题描述】:

我有一个奇怪的问题。我的应用程序在装有 Android 2.3.4 的三星 Galaxy II S 上运行良好。在我的装有 Android 3.0 的平板电脑上,出现了一种奇怪的行为:

突然显式设置为白色的背景颜色是不同的灰色。

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

以下几点可能会导致这种行为:

  • 使用 Web 视图。它并非一直使用,而且似乎这种行为发生得更快/仅当 WebView 可见时才会发生
  • 改变方向似乎会推动这种行为

到目前为止我所尝试的:

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

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

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

简单活动的代码

<?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 完成活动的代码

<?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>

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

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

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

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

在此先感谢

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以尝试将其用于纯色(此处为白色)颜色背景

    android:cacheColorHint="#ffffffff"
    

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

    android:cacheColorHint="#00000000"
    

    让我知道这是否有效

    【讨论】:

    • 不幸的是它没有帮助。我在所有相关的 LinearLayout 等中添加了 cacheColorHint (#ffffffff)。(特别有趣,因为简单的活动只有一个带有 4 个按钮的 LinearLayout),它们仍然变成灰色。
    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2016-11-24
    • 2013-11-27
    • 1970-01-01
    • 2013-10-10
    相关资源
    最近更新 更多