【问题标题】:Why is setBackgroundColor() failing only with my custom theme?为什么 setBackgroundColor() 仅在我的自定义主题中失败?
【发布时间】:2012-07-09 04:55:53
【问题描述】:

我以为我成功地创建了一个条纹 Android ListView,其中列表项的背景颜色交替出现。当我使用 Theme.Holo.Light 作为我的应用程序的主题(在清单中)时,它可以完美地工作。 但是,当我为我的应用程序定义一个自定义主题时,使用自定义背景颜色,条纹消失了,取而代之的是我的主题中定义的单一背景颜色。 为什么 getView() 中的 setBackgroundColor() 无法覆盖我自定义主题的背景颜色?我该如何解决这个问题?谢谢!

更新:我已经意识到应用程序背景正在呈现在我的视图背景之前! (另外,在我的布局中的 ProgressBar 前面,完全遮住它。)如果我将 @color/background_color 设置为完全透明,则条纹会显示出来。那么,问题来了,为什么我的主题的背景渲染在一些视图/背景前面?

主题:

<style name="Basic">
    <item name="android:textColor">#000000</item>
    <item name="android:background">@color/background_color</item>
</style>

getView():

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout itemView;
        Resources res = getResources();
        int[] colors = new int[] {res.getColor(R.color.list_stripe_1),res.getColor(R.color.list_stripe_2)};
        if (convertView == null){
            itemView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater viewInflater;
            viewInflater = (LayoutInflater)getContext().getSystemService(inflater);
            viewInflater.inflate(resource, itemView, true);
        } else {
            itemView = (LinearLayout) convertView;
        }

        ////Omitting code for populating TextViews..

        itemView.setBackgroundColor(colors[position%colors.length]);
        return itemView;            
    }   

【问题讨论】:

    标签: java android styles themes


    【解决方案1】:

    不要在主题中使用设置 android:background,而是设置 android:windowBackground。我还没有完全调查为什么无法使用 setBackgroundColor() 覆盖主题集背景,或者为什么使用 android:background 会导致微调器被遮挡,但使用 windowBackground 解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多