【问题标题】:Android Action Bar : Custom tabs and overflowAndroid 操作栏:自定义选项卡和溢出
【发布时间】:2011-03-21 00:02:33
【问题描述】:

我很难为操作栏实现自定义样式的选项卡:我需要使选项卡(我的意思是按钮)在正常和选定状态下都使用自定义图形。

我已经能够使用

杀死所有原生样式
<style name="customActionBarTabStyle">
    <item name="android:background">#00000000</item>
</style>

然后使用 Tab.setIcon() 使标签看起来像我需要的那样,但我需要它们对切换做出反应(通过在两个 Drawable 之间切换 - 用于打开和关闭状态)。

我试过像这样创建一个 Drawable 选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/btn_on" />
    <item android:drawable="@drawable/btn_off" />
</selector>

但选项卡在选择时不会切换到按下模式。

另外,我尝试在 TabListener.onTabSelected() 和 .onTabUnselected() 中调用 Tab.setIcon() - 也没有运气。

有人知道解决这个问题的好方法吗?

另外,我需要显示自定义视图而不是溢出菜单 - 我已经搜索了一堆“提示”来重新考虑我的 UI 以“遵循 Android 方式”,但问题是 UI 实际上不是我的重新考虑 - 它是客户的 :) 所以我真的很想找到一种方法来解决 ActionBar 的自定义缺点。

任何建议都非常感谢,在此先感谢。

【问题讨论】:

  • 您使用的是完全没有背景的自定义图标吗?你想要完成什么样的外观?
  • 不,&lt;style name="customActionBarTabStyle"&gt; &lt;item name="android:background"&gt;#00000000&lt;/item&gt; &lt;/style&gt; 覆盖默认选项卡背景 - 以摆脱本机带下划线的外观,并且选择器在透明选项卡背景上绘制为选项卡图标。这与 ActionBar 自己的背景无关 - 它就在那里,并且绘制在选项卡图标和透明选项卡背景下。

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


【解决方案1】:

尝试使用 state_selected 而不是 state_pressed,只是尝试在此处执行类似的操作(完全更改选项卡的背景图像),并且操作栏选项卡似乎只进入选定的真/假而不是按下状态...

编辑:似乎背景属性进入该状态,但不是选项卡上使用的文本颜色...

【讨论】:

  • 嗯,谢谢,我一定会尝试看看它是否有效)实际上,通过将自定义视图应用于选项卡而不是自定义 Drawables,问题得到了解决(不幸的是,不是我)。 ImageView tab = (ImageView) (getLayoutInflater().inflate(R.layout.mytab, null)); getActionBar().newTab().setCustomView(tab); 然后在选项卡侦听器的 onTabSelected()/onTabUnselected() 方法中切换按下/未按下的图标,就像这样 ((ImageView) tab.getCustomView()).setImageDrawable(...) 您是否还尝试为状态使用不同的文本颜色?这种方法也可能有所帮助。
  • 要让按下状态生效,您需要在自定义选项卡视图中的任何子视图中添加:android:duplicateParentState="true"。
【解决方案2】:

我认为你必须使用 android:selectableItemBackground 看看this 它可能会有所帮助。

祝你好运

【讨论】:

    【解决方案3】:

    只是为了让解决方案更加呈现:

    @taf 表示解决方案:您需要在您用作标签的自定义视图的父布局中设置android:duplicateParentState="true"

    【讨论】:

      【解决方案4】:

      这是我的代码,选择选项卡时图标会发生变化,未选择时会变回来。

          public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             requestWindowFeature(Window.FEATURE_ACTION_BAR);
             setContentView(R.layout.activity_main);
      
             // Set up the action bar.
             final ActionBar actionBar = getActionBar();
             actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
             actionBar.setBackgroundDrawable(null);
      
          icons = new int[] {
                  R.drawable.ic_facebook,
                  R.drawable.ic_facebook,
                  R.drawable.ic_facebook,
                  R.drawable.ic_facebook,
          };
          icons_selected = new int[] {
                  R.drawable.ic_launcher,
                  R.drawable.ic_launcher,
                  R.drawable.ic_launcher,
                  R.drawable.ic_launcher,
          };
      
      
          // For each of the sections in the app, add a tab to the action bar.
          for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
              // Create a tab with text corresponding to the page title defined by
              // the adapter. Also specify this Activity object, which implements
              // the TabListener interface, as the callback (listener) for when
              // this tab is selected.
              actionBar.addTab(
                      actionBar.newTab()
                              .setText(mSectionsPagerAdapter.getPageTitle(i))
                              .setIcon(icons[i])
                              .setTabListener(this)
              );
          }
          }
      

      还有这个

          @Override
          public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction){
      
          getActionBar().getSelectedTab().setIcon(icons_selected[tab.getPosition()]);
          }
      
          @Override
          public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
          getActionBar().getSelectedTab().setIcon(icons[tab.getPosition()]);
          }
      
          @Override
          public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
          }
      

      【讨论】:

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