删除图标/标题动作条与扩大搜索查看? [英] Remove icon/title for actionbar with expanded SearchView?

查看:167
本文介绍了删除图标/标题动作条与扩大搜索查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的项目,瞄准API 11以上。我有我想要在默认情况下扩张了搜索查看活动:

  @覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
    super.onCreateOptionsMenu(菜单,充气);

    inflater.inflate(R.menu.menu_search,菜单);
    菜单项searchItem = menu.findItem(R.id.action_search);
    mSearchView =(搜索查看)searchItem.getActionView();
    mSearchView.setIconifiedByDefault(假);
}
 

这工作,但我不想在动作条的任何图标或标题,只是搜索查看。我试图得到做掉我的标题和图标:

  getActionBar()setDisplayHomeAsUpEnabled(假)。
getActionBar()setDisplayShowTitleEnabled(假)。
getActionBar()的setIcon(android.R.color.transparent)。
 

但我的动作条依然有这个填充到搜索查看左:

有没有办法摆脱它?

感谢

--------编辑--------------------

下面是我的活动在全:

 公共类GreatAndroid扩展FragmentActivity {
    私人搜索查看mSearchView;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        getActionBar()setDisplayShowHomeEnabled(假)。
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        super.onCreateOptionsMenu(菜单);

        。getMenuInflater()膨胀(R.menu.menu_search,菜单);
        菜单项MI = menu.findItem(R.id.action_search);
        mSearchView =(搜索查看)mi.getActionView();
        mSearchView.setIconifiedByDefault(假);

        返回true;
    }
}
 

和这里的菜单的xml:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
   <项目机器人:ID =@ + ID / ACTION_SEARCH
      机器人:标题=@字符串/搜索
      机器人:图标=@机器人:可绘制/ ic_menu_search
      机器人:showAsAction =总是
      机器人:actionViewClass =android.widget.SearchView/>
< /菜单>
 

解决方案

尽管图标是透明的,但它仍然占用空间:

  getActionBar()的setIcon(android.R.color.transparent)。
 

删除此行并添加:

  getActionBar()setDisplayShowHomeEnabled(假)。
 

现在,你应该有搜索查看占用所有的可用空间。

这是可以折叠动作的项目有一个最大宽度。唯一的解决办法我能想到的是以下内容:

 公共类GreatAndroid扩展FragmentActivity {
    私人搜索查看mSearchView;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        getActionBar()setDisplayShowHomeEnabled(假)。
        getActionBar()setDisplayShowTitleEnabled(假)。
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        super.onCreateOptionsMenu(菜单);

        。getMenuInflater()膨胀(R.menu.menu_search,菜单);
        菜单项MI = menu.findItem(R.id.action_search);
        mSearchView =(搜索查看)mi.getActionView();
        mSearchView.setIconifiedByDefault(假);

        //你可以使用Display.getSize(点)的API 13日起。
        //对于API 11和12中,使用Display.getWidth()

        最后的点P =新的点();

        。getWindowManager()getDefaultDisplay()的getSize(P)。

        //创建的LayoutParams宽度设置为屏幕的宽度
        的LayoutParams PARAMS =新的LayoutParams(PX,LayoutParams.MATCH_PARENT);

        mSearchView.setLayoutParams(PARAMS);

        返回true;
    }
}
 

虽然看起来还是有空间的左边,你可以使用 mSearchView.setBackgroundColor(Color.RED); 一看就知道是不是真的有。

I created a new project, targeting api 11 and above. I have an activity where I want a SearchView expanded by default:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search); 
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setIconifiedByDefault(false);
}

which works, but I don't want any icon or title on the actionbar, just the searchview. I tried getting rid of my title and icon by doing:

getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(android.R.color.transparent);

But my actionbar still has this padding to the left of the searchview:

Is there a way to get rid of it?

Thanks

-------- Edit --------------------

Here's my activity in full:

public class GreatAndroid extends FragmentActivity {
    private SearchView mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem mi = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) mi.getActionView();
        mSearchView.setIconifiedByDefault(false);

        return true;
    }
}

and here's the menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/action_search"
      android:title="@string/search"
      android:icon="@android:drawable/ic_menu_search"
      android:showAsAction="always"
      android:actionViewClass="android.widget.SearchView" />
</menu>

解决方案

Even though the icon is transparent, it still takes up the space:

getActionBar().setIcon(android.R.color.transparent);

Remove this line and add:

getActionBar().setDisplayShowHomeEnabled(false);

Now, you should have the SearchView take up all the available space.

Action items that can be collapsed have a max width. The only workaround I can think of is the following:

public class GreatAndroid extends FragmentActivity {
    private SearchView mSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem mi = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) mi.getActionView();
        mSearchView.setIconifiedByDefault(false);

        // You can use Display.getSize(Point) from API 13 onwards.
        // For API 11 and 12, use Display.getWidth()

        final Point p = new Point();

        getWindowManager().getDefaultDisplay().getSize(p);

        // Create LayoutParams with width set to screen's width
        LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);

        mSearchView.setLayoutParams(params);

        return true;
    }
}

Although it looks like there's still space to the left, you can use mSearchView.setBackgroundColor(Color.RED); to see that there really isn't.

这篇关于删除图标/标题动作条与扩大搜索查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆