Android的动作条 - 推自定义视图屏幕下方自定义、视图、屏幕、动作

2023-09-12 01:49:26 作者:做你心上人

我有一个分裂的ActionBar,而我想补充的功能几乎相同的Google Play中的正在播放。

I've got a split ActionBar, and I'm trying to add functionality almost identical to google play's 'Now playing'..

我能得到的菜单项出现在屏幕的底部,使用onCreateOptionsMenu,但我似乎无法获得自定义视图使用actionBar.setCustomView时,出现在屏幕的底部。自定义视图只是坐在上面动作条的下方。

I can get menu items to appear at the bottom of the screen, using the onCreateOptionsMenu, but I can't seem to get a custom view to appear at the bottom of the screen when using actionBar.setCustomView. The custom view just sits underneath the top ActionBar.

有谁知道如何强制customview底部,或添加自定义视图到onCreateOptionsMenu?

Does anyone know how to force the customview to the bottom, or to add custom views to the onCreateOptionsMenu?

下面是我的code中的片段:

Here are the snippets of my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    //Custom view to add
    actionBar.setCustomView(R.layout.albumitem);
            //This view shows up just under the top actionbar at the moment,
              despite menu items being at the bottom

该菜单选项code:(这些都呈现下跌的底部在哪里,我想我的自定义视图是)

The menu options code: (These are showing down the bottom where I'd like my custom view to be)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.option_menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

我相信,在谷歌播放音乐的应用程序的底部正在播放菜单拆分操作栏里面的自定义视图:

I believe that the now playing menu at the bottom of the google play music app is a custom view inside a split action bar:

推荐答案

所以我设法找到一个肮脏的解决这个......嗯,脏我的业余看法。

So I managed to find a dirty solution to this.. Well, dirty in my amateurish opinion..

在oncreateOptionsMenu,我已经膨胀了一个名为option_menu像这样的菜单:

In the oncreateOptionsMenu, I've inflated a menu called option_menu like so:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);

而在XML为option_menu项目,我已经实现了actionViewClass,在这种情况下,我用相对布局(我猜我可能只需要使用浏览..?):

And in the xml for that option_menu item, I've implemented an actionViewClass, and in this case I've used relative layout (I'm guessing I could've just used View..?):

<item
    android:id="@+id/layout_item"
    android:actionViewClass="android.widget.RelativeLayout"
    android:showAsAction="always|withText"
    android:title="Text 1"/>

</menu>

于是我膨胀的XML布局,并将其添加到我的菜单中定义的layout_item:

So then I inflated an xml layout, and added it to the layout_item defined in my menu:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);
        relativeLayout = (RelativeLayout) menu.findItem(R.id.layout_item)
                .getActionView();

        View inflatedView = getLayoutInflater().inflate(R.layout.now_playing,
                null);

        relativeLayout.addView(inflatedView);

        return (super.onCreateOptionsMenu(menu));
    }

请随意编辑/增强这个答案,你认为合适。

Please feel free to edit/enhance this answer as you see fit.