简体   繁体   English

ActionBar中的SearchView和Activity Title

[英]SearchView and Activity Title in ActionBar

Very simple question that doesn't need much code: 非常简单的问题,不需要太多代码:

I'm using Android's default ActionBar (no Sherlock) in which I have a couple of MenuItems. 我正在使用Android的默认ActionBar(没有Sherlock),其中我有几个MenuItems。 One of them is a collapsible Action View ( android:showAsAction="collapseActionView|always" ) for your typical search scenario: user clicks on the icon → a search box expands → when the user types anything onQueryTextChange does its magic). 其中一个是针对典型搜索场景的可折叠动作视图( android:showAsAction="collapseActionView|always" ):用户点击图标→搜索框展开→当用户键入任何内容时,onQueryTextChange会发挥其魔力)。

SearchView sv = new SearchView(this);
sv.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
// Yada, yada...
sv.setOnQueryTextListener(new OnQueryTextListener() {
    // The methods for onQueryTextSubmit and onQueryTextChange ... they apply filtering on the activity's list adapter and apparently work fine
}
menuItem.setActionView(sv); // menuItem is the item for the search action
searchAnswers.setOnActionExpandListener(new OnActionExpandListener() {
    // The listener's methods for expand/collapse as I need some business logic behind them
}

All of the above works fine. 所有上述工作都很好。

When the device is on portrait mode and has limited screen space, the SearchView occupies pretty much the whole action bar, hiding the other MenuItems (which have android:showAsAction="ifRoom" ) as well as the Activity's title, which is OK by me. 当设备处于纵向模式且屏幕空间有限时,SearchView几乎占据了整个操作栏,隐藏了其他MenuItems(其中包含android:showAsAction="ifRoom" )以及Activity的标题,这对我来说还可以。 。

The problem is that if the device is on landscape mode (so that there's still free ActionBar space), the SearchView doesn't occupy the whole ActionBar (again, fine by me) but the Activity's title disappears (even though there's plenty of space where it could be displayed!). 问题是,如果设备处于横向模式(因此仍然有免费的ActionBar空间),SearchView不会占用整个ActionBar(再次,我很好),但Activity的标题消失了 (即使有足够的空间在哪里它可以显示!)。 So my problem is that I get this empty space where the Activity's title could be shown. 所以我的问题是我得到了这个空白的空间,可以显示Activity的标题。

Before expanding: 在扩展之前: 有活动标题。

After expanding: 扩展后: 我可以租用这么多的免费空间。

(Both screenshots are from a phone in landscape mode. Tested with Android 4.0.4 and 4.3.) (这两个截图都来自横向模式的手机。使用Android 4.0.4和4.3进行测试。)

Any tips on how to keep the Activity's title when there's enough space? 有足够空间时如何保持活动标题的任何提示?

Not sure if you found an answer to this problem yet. 不确定你是否找到了这个问题的答案。 But I found an answer on this thread that may help - it certainly helped me. 但是我在这个帖子上找到了一个可能有帮助的答案 - 这对我有帮助。

ActionBar SearchView not fully expanding in landscape mode ActionBar SearchView未在横向模式下完全展开

Code copied from link: 从链接复制的代码:

searchView.setOnSearchClickListener(new OnClickListener() {
    private boolean extended = false;

    @Override
    public void onClick(View v) {
        if (!extended) {
        extended = true;
            LayoutParams lp = v.getLayoutParams();
            lp.width = LayoutParams.MATCH_PARENT;
        }
    }
});
searchView.setMaxWidth(Integer.MAX_VALUE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM