【发布时间】: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上?等等。你能在其他设备/模拟器上测试以确认它只在那个设备上吗?