【问题标题】:How to programatically change the background color of the stacked bar on API v11+ using android.support.v7.app.ActionBar?如何使用 android.support.v7.app.ActionBar 以编程方式更改 API v11+ 上堆叠条的背景颜色?
【发布时间】:2014-03-04 12:31:42
【问题描述】:

我知道我可以在 xml 中执行此操作,但我想以编程方式执行此操作。

我正在使用 bar.setBackgroundDrawable(new ColorDrawable(0x20000000 + currentlySelectedLine.color));bar.setStackedBackgroundDrawable(new ColorDrawable(0x20000000 + currentlySelectedLine.color)); 更改 actionBar 的背景颜色。

在 values,values-v11 和 values-v14 我有相同的风格 - 指向 Theme.AppCompat.Light 的父级,没有任何内容。

在下图中,您可以看到当我在 API lvl 10 的模拟器上运行程序时会发生什么。我是否使用 setStackedBackgroundDrawable 方法似乎没有区别。

在下图中,您可以看到当我在 API lvl 11 的模拟器上运行程序时会发生什么。我是否使用 setStackedBackgroundDrawable 方法,似乎没有区别。

所以。我的问题是:您能告诉我应该怎么做才能使 API lvl 11+ 设备的选项卡后面出现粉红色背景吗?

【问题讨论】:

    标签: android-3.0-honeycomb android-tabs android-actionbar-compat


    【解决方案1】:

    查看android.support.v7 的源代码,我发现android.support.v7.app.ActionBar.setStackedBackgroundDrawable() 实际上什么也没做。

    由于我一直在使用android.support.v7.app.ActionBarActivity,并且在 Honeycomb 上进行了大部分测试,我认为我将无法使用 android.app.Activity.getActionBar(),因为该方法会返回 null。

    但在 4.1.2 中并非如此。在 4.1.2 中,使用该方法,我得到了 android.app.ActionBar。

    看着this webpage,我意识到使用 API lvl 11-13 的 Android 用户或多或少有 0.1%。

    所以,我修改了我的代码,如下所示:

      // activity is an instance of ActionBarActivity.
      // ActionBar = android.support.v7.app.ActionBar.
      ActionBar bar = activity.getSupportActionBar();
      bar.setBackgroundDrawable(new ColorDrawable(0x20000000 +
          currentlySelectedLine.color));
        if (android.os.Build.VERSION.SDK_INT > 13)
          setBackgroundColorV14(activity);
    
      // ...
    
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void setBackgroundColorV14(final ActionBarActivity activity) {
      android.app.ActionBar bar = activity.getActionBar();
      bar.setStackedBackgroundDrawable(new ColorDrawable(0x20000000 +
          currentlySelectedLine.color));
    }
    

    基本上,结果是我在 API v7-10 和 API v14+ 上的选项卡后面确实有背景颜色。

    【讨论】:

    • 你真的救了我,我知道它很旧,但你知道如何自定义下划线选项卡的颜色吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多