4

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

我已经能够使用杀死所有原生样式

<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 的自定义缺点。

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

4

4 回答 4

1

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

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

于 2011-06-24T11:02:40.947 回答
0

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

祝你好运

于 2012-02-22T14:09:31.093 回答
0

这是我的代码,选择选项卡时图标会更改,未选中时会更改回来。

    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) {
    }
于 2014-06-20T20:04:56.950 回答
0

只是为了使解决方案更加存在:

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

于 2012-08-15T15:52:51.963 回答