【问题标题】:Custom background for activatedBackgroundIndicator on ActionBarSherlock doesn't workActionBarSherlock 上的activatedBackgroundIndicator 的自定义背景不起作用
【发布时间】:2013-03-11 17:02:07
【问题描述】:

我正在使用 ActionBarSherlock,并且正在尝试为行背景自定义 activatedBackgroundIndicator 属性。

如果我使用最新的 android sdk,没有 ActionBarSherlock,我可以自定义背景,在 res/values/style.xml 上创建以下样式和在 AndroidManifest.xml 上将其定义为 android:theme="@style/Theme.Custom"

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Custom" parent="android:Theme.Holo.Light.DarkActionBar">
     <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
  </style>
</resources>

然后,我的 res/drawable/activated_background.xml 包含下一个代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_activated="true" android:drawable="@color/row_activated" />
   <item android:drawable="@android:color/transparent" />  
</selector>

最后,用于定义ListView中每一行的代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="?android:attr/activatedBackgroundIndicator">
    <TextView
            android:id="@+id/test_row"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/sample_string"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:padding="15dp"/>
</LinearLayout>

结果显示在屏幕截图中。是一个简单的应用程序,在单击列表项时只有一个 ListView 和 ListView.CHOICE_MODE_SINGLEgetListView().setItemChecked(position, true)

标准选定行的蓝色现在是黄色并且可以完美运行。

当我想使用 ActionBarSherlock 应用相同的自定义时,就会出现问题。 现在样式文件是下一个,背景显示为蓝色而不是自定义的黄色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Custom" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="activatedBackgroundIndicator">@drawable/activated_background</item>
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>

    <style name="activatedBackgroundIndicator">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>
</resources>

我不知道 ActionBarSherlock 是否支持 android:activatedBackgroundIndicator 功能,或者我是否忘记实现一些需要能够更改默认颜色的东西。

有什么想法吗?

【问题讨论】:

  • ActionBarSherlock 确实支持activatedBackgroundIndicator 属性。另外,为什么你的 XML 中定义了一个名为“activatedBackgroundIndicator”的样式?
  • 我搜索了实现此功能的项目,发现Put.io 正在使用它。
  • 我添加了样式以使其正常工作,但如果没有必要,我将删除它。那么@Alex-Fu,使用ActionBarSherlock的实现必须和默认一样?
  • 除非您将activatedBackgroundIndicator 称为样式,否则您不需要样式定义。 Theme.Custom 定义中的所有内容都是正确的。如果在 LinearLayout 中将 android:background="?android:attr/activatedBackgroundIndicator" 更改为 android:background="?attr/activatedBackgroundIndicator" 会发生什么?
  • 此外,您应该在可绘制选择器中包含尽可能多的状态。 android:state_activated="true" 不足以涵盖所有可能性,例如长按、点击、聚焦等...

标签: android android-layout background actionbarsherlock android-theme


【解决方案1】:

终于找到了解决问题的办法。
它是行适配器中使用的 context。使用的构造函数如下代码所示:

public RowAdapter(Activity activity, ArrayList<String> list) {
    this.mContext = activity.getApplicationContext();
    this.elements = list;
    this.theActivity = activity;

    this.inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

只需将 上下文 更改为:
this.mContext = activity.getApplicationContext()(Application-Context)

this.mContext = activity.getBaseContext()(活动上下文)
解决了问题,现在背景是自定义的。

当您需要操作视图时,请使用 Activity-Context,否则 Application-Context 就足够了。

answer 帮助我理解了为什么在我的例子中使用 getBaseContext() 而不是 getApplicationContext()

【讨论】:

    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多