【问题标题】:Unwanted background gradient in ListFragmentListFragment 中不需要的背景渐变
【发布时间】:2013-11-11 14:11:01
【问题描述】:

我有一个应用程序,它是由 Android 向导在 Eclipse 中构建的,用于带有片段的主/详细信息流。它使用Theme.Holo.Light 主题作为父方案,并包含最新的support-v4 库。

我需要自定义ListFragment 的颜色,使激活的项目具有与视图的默认背景相同的背景颜色,而所有其他项目具有另一种自定义颜色。所以我做了以下事情。

styles.xml

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>

drawable/activated_background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_activated="true" android:drawable="@color/white" />
   <item android:drawable="@color/someothercolor" />
</selector>

color.xml 包含(除其他外):

<color name="background_holo_light">#f3f3f3</color>
<color name="background_holo_light2">#e8e8e8</color>
<color name="white">#FFFFFF</color>

问题

显然应用了指定的颜色,但看起来它在列表中从上到下变得越来越密集。例如,第一个项目是雪白色,但第 7 个项目具有类似于holo_light_background 的浅灰色背景(与列表右侧显示的详细信息面板相比,这很明显)。如果我使用holo_light_background 颜色,这种不一致会变得更加明显,因为最后一项具有深灰色背景。

简而言之,列表背景演示了一个渐变,这在我的代码中没有指定

适配器代码列表中,只有:

public View getView(int position, View convertView, ViewGroup parent)
{
  View view = super.getView(position, convertView, parent);
  TextView tv = (TextView)view.findViewById(android.R.id.text1);
  // get item by position...
  tv.setText(item.title);
  return view;
}

用于单个项目的布局使用LinearLayoutactivatedBackgroundIndicator 背景:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightLarge"
    >
... child controls are omitted

onViewCreated 也很简单:

@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
  super.onViewCreated(view, savedInstanceState);

  setActivateOnItemClick(true);
  setActivatedPosition(mActivatedPosition);
}

问题

渐变是从哪里来的?要么我需要在我的主题中覆盖一个隐式样式属性,要么列表中的项目有一些秘密 alpha(透明度值),随着每个项目的创建而倍增。

如何消除这种行为?

【问题讨论】:

    标签: android background gradient android-listfragment


    【解决方案1】:

    我已经设法解决了这个问题。该解决方案包括两个步骤。

    首先,我将应该是激活项的颜色的颜色应用到整个ListView作为背景颜色:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
      super.onViewCreated(view, savedInstanceState);
      getListView().setBackgroundColor(getResources().getColor(R.color.background_holo_light));
    }
    

    其次,我将 activated_background 选择器更改为对已激活的项目使用透明颜色。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">  
       <item android:state_activated="true" android:drawable="@android:color/transparent" />
       <item android:drawable="@color/someothercolor" />
    </selector>
    

    现在看起来和预期的一样。唯一的缺点是项目下的可用空间以与激活项目相同的颜色显示。这不好,这就是为什么我想以其他方式解决问题。

    经过一些进一步的调查和测试,我现在可以确认效果确实是平滑应用到所有ListView 项目的渐变。此外,它似乎也影响了所有其他视图的背景。这在某种程度上与Theme.Holo.Light 主题有关。我通过以下方式更改了应用样式:

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowBackground">@color/white</item>
    </style>
    

    这完全解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      相关资源
      最近更新 更多