【问题标题】:Textview changes background color for no reasonTextview无缘无故改变背景颜色
【发布时间】:2015-01-05 21:54:31
【问题描述】:

我在一个活动中有两个文本视图,由 xml 定义 - 都具有灰色背景色。在我的应用程序中,我将其中一种 textviews 背景颜色设置为蓝色。这按预期工作。

但是:当我转动设备(旋转)或离开应用程序并再次返回时,另一个文本视图也是蓝色的 - 与故意设置的颜色相同......!?

当我离开应用程序并再次启动它时,第二个文本视图保持蓝色。当我停止应用程序运行(终止)并再次启动它时,第二个文本视图是灰色的。但是,当我下次旋转设备或启动应用程序时,就会出现同样的问题。

问题设备正在运行 4.1.1。 - 在 2.3.4 设备上运行相同的应用程序没有问题。

SDK 工具 22.0.1,Eclipse Juno Service Release 2 32 位 , Windows 7 64 位

编辑: SDK 工具 14 上的相同问题,Windows 7 32 位上的 Eclipse Indigo SR1 32 位

我不知道那里发生了什么。这是某种不受欢迎的魔法。 你能帮帮我吗?

以下是问题项目中未经修改的真实源代码。

MainActivity.java:

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv1 = (TextView) findViewById(R.id.textView1);

        tv1.setBackgroundColor(0xff33b5e5);

    }

}

acitivity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:background="#cccccc" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="TextView Test" >
        <activity android:name="com.example.test.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

编辑 2: 让事情变得更奇怪:如果我将 textview2 的颜色稍微更改为 #cdcdcd 问题就不会出现。只有在两种颜色(textview1 和 textview2)在 XML 中相同的情况下。

【问题讨论】:

  • 尝试干净并重建您的项目
  • 我已经这样做了大约 100 次......! :-( 上面的代码是我真实项目的摘要。制作该摘要花费了我几个小时,同时不断刷新和重建以查看问题是否仍然存在。
  • :(我看不出上面的代码有什么问题,如果你发现发布的代码真的很奇怪的话。希望其他人能提供帮助。
  • 我同意这很奇怪!因为上面贴出的代码是100%。
  • 所以问题只出在一台设备上?那一定是那个设备有问题。尝试完全卸载应用程序,然后重新安装。设备有什么奇怪的地方吗?在股票ROM上?等等。你能在其他设备/模拟器上测试以确认它只在那个设备上吗?

标签: android textview


【解决方案1】:

我找到了解决该问题的方法 - 尽管不是解释。只有当 xml 中两个 textview 的初始颜色相同时,才会存在问题。因此,解决方案是为 textviews 赋予不同的颜色。

所以,如果你有同样的问题,这对我有用:

acitivity_main.xml:有问题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="#cccccc" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:background="#cccccc" />

</LinearLayout>

acitivity_main.xml:没有问题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="#ffcccccc" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:background="#fecccccc" />

</LinearLayout>

换句话说,我只使用了稍微不同的颜色(实际上这里是不同的透明度)——问题就消失了。如果有人告诉我,我不会相信。

【讨论】:

    【解决方案2】:

    真的太棒了:) 试试类似的东西-

    public class MainActivity extends Activity{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView tv1 = (TextView) findViewById(R.id.textView1);
    
            tv1.setBackgroundColor(0xff33b5e5);
    
           TextView tv2 = (TextView) findViewById(R.id.textView2);
    
            tv2.setBackgroundColor(Color.Red);
    
        }
    
    }
    

    【讨论】:

    • 请更正颜色... ccccccc 有点奇怪的十六进制值... ;-) 仅适用于复制代码的任何人。
    【解决方案3】:

    我想我偶然发现了一个与 android 4.1.1 类似的错误。经过几个小时的头痛后,我发现当您在 XML 中为元素声明颜色,然后以编程方式对其进行编辑时,就会出现问题。我设法通过不在 xml 中为要更改背景颜色的元素声明颜色来摆脱它。

    【讨论】:

      猜你喜欢
      • 2013-11-03
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多