【发布时间】: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;
}
用于单个项目的布局使用LinearLayout 和activatedBackgroundIndicator 背景:
<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