android – LayoutInflater有时(随机)膨胀错误的背景颜色

我使用LayoutInflater扩展视图.我的膨胀RelativeLayout的背景颜色在我的xml文件中设置.我在我的一个设备上遇到了一个奇怪的问题:有时(随机)背景颜色是我的colors.xml中的另一种(错误的)颜色.以前有人遇到过这个问题吗?

细节:

我有一个带有CursorAdapter的ListView.我使用以下代码使用一个静态项目(所以我认为这不是回收问题)来扩充列表项目:

@Override
public View newView(Context context,Cursor cursor,ViewGroup parent) {
    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
    View v = vi.inflate(R.layout.bookmark_item,null) ;
    //bindView(v,context,cursor) ;
    Log.wtf("newView","View color: " + Integer.toString(((ColorDrawable) (((RelativeLayout)v.findViewById(R.id.bookmark_row)).getBackground())).getColor())) ;
    return v;
}

我的布局/ bookmark_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/bookmark_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_menu_item_background"
        tools:ignore="Uselessparent" >

        <TextView 
            android:id="@+id/bookmark_item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>

</RelativeLayout>

内部RelativeLayout的背景颜色设置为@ color / app_menu_item_background.这个颜色在我的R.java中:

public static final int app_menu_item_background=0x7f0a0036;

我还有一个名为@ color / app_menu_item_colored_background的颜色,我在代码中的其他地方使用的颜色,它与我的书签列表和适配器没有任何共同之处.它在R.java中也有不同的资源ID:

public static final int app_menu_item_colored_background=0x7f0a0038;

它们都是不同的颜色:

<color name="app_menu_item_background">#517409</color>
<color name="app_menu_item_colored_background">#f6efde</color>

然后,在运行我的应用程序时,有时(并非总是),我的View使用了错误的app_menu_item_colored_background背景颜色.我记录了膨胀的视图颜色(参见上面的代码),它甚至有时不同:

"newView" - "View color: -11439095"
"newView" - "View color: -593954"

注意,第一种颜色是#517409,第二种颜色是#f6efde.

奇怪的是,我只能在一台设备上重现错误,三星galaxy S3 mini,每10次尝试大约2-3次.

解决方法

手动尝试
v.setBackgroundColor(getResources().getColor(R.color.app_menu_item_colored_background));

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...