Android 操作栏:自定义选项卡和溢出

发布于 2024-10-24 19:03:02 字数 950 浏览 1 评论 0原文

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

我已经能够使用 Tab.setIcon() 杀死所有本机样式

<style name="customActionBarTabStyle">
    <item name="android:background">#00000000</item>
</style>

,然后使用 Tab.setIcon() 使选项卡看起来像我需要的方式,但我需要它们对切换做出反应(通过在两个 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 的定制缺点。

非常感谢任何建议,提前致谢。

I'm having a hard time implementing custom-styled tabs for action bar: I need to make tabs (I mean buttons) use custom graphics for both normal and selected states.

I've been able to kill all the native styling using

<style name="customActionBarTabStyle">
    <item name="android:background">#00000000</item>
</style>

and then use Tab.setIcon() to make the tabs look the way I need, but I need them to react to switching (by switching between two Drawables - for on and off state).

I've tried creating a Drawable selector like this:

<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>

but the tabs don't switch to pressed mode when selected.

Also, I've tried calling Tab.setIcon() in TabListener.onTabSelected() and .onTabUnselected() - no luck either.

Does anyone know a good solution to this one?

Also, I need to display a custom view instead of overflow menu - I've already googled up a bunch of "hints" to rethink my UI to "follow the Android way" but the problem is the UI is not actualy mine to rethink - it's customer's :) so I'd really like to find a way to workaround ActionBar's customization shortcomings.

Any suggestions are much appreciated, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぃ双果 2024-10-31 19:03:03

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

编辑:似乎就像背景属性进入该状态一样,但选项卡上使用的文本颜色没有进入...

Try using state_selected instead of state_pressed, just trying to do something similar here (changing the background images for the tabs completely) and it seems like that the action bar tabs only enter selected true/false but not the pressed state...

EDIT: Seems like the background property enters that state, but not the text color used on the tabs...

爱人如己 2024-10-31 19:03:03

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

祝你好运

i think that you'll have to use android:selectableItemBackground
have a look at this it may help.

Good luck

深爱成瘾 2024-10-31 19:03:03

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

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

Just to make the solution more present:

@taf said the solution: you need to set the android:duplicateParentState="true" in the parent layout you use as a custom view for your tab.

吝吻 2024-10-31 19:03:03

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

    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) {
    }

Here is my code and the icon change when selected a tab, and change back when unselected.

    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)
        );
    }
    }

and 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) {
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文