【问题标题】:android-How to add search view to custom actionbarandroid-如何将搜索视图添加到自定义操作栏
【发布时间】:2014-12-16 05:21:50
【问题描述】:

我正在为我的应用程序使用自定义操作栏。 这是代码:

private void actionbar() {
    ActionBar actionBar = getSupportActionBar();
    LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mCustomView = layoutInflater.inflate(R.layout.actionbar, null);

    ImageView nDrawer=(ImageView)mCustomView.findViewById(R.id.imageView4);
    nDrawer.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            DrawerActivity.open();              
        }
    });

    ///search

    actionBar.setCustomView(mCustomView);
    actionBar.setDisplayShowCustomEnabled(true);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
     actionBar.setDisplayShowTitleEnabled(false);
     actionBar.setDisplayHomeAsUpEnabled(true);
     actionBar.setDisplayUseLogoEnabled(false);
     actionBar.setDisplayShowHomeEnabled(false);
     actionBar.setHomeButtonEnabled(true);
     actionBar.setIcon(null);
}

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/menubg"
    android:paddingLeft="3dp"
    android:paddingRight="3dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_action_search" />
</RelativeLayout>

我想使用像 android 的默认搜索视图这样的搜索视图(我的意思是像菜单上的搜索图标,当点击它时,它会变成一个编辑文本)

如何将搜索视图添加到我的自定义操作栏?

【问题讨论】:

    标签: android android-actionbar android-custom-view android-actionbar-compat


    【解决方案1】:

    在自定义布局中使用此小部件

    <android.support.v7.widget.SearchView
            android:suggestionRowLayout="@layout/suggestion_row_layout"
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:iconifiedByDefault="false"
            android:inputType="textFilter"/>
    

    比:

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            mSearchModeOn = true;
            mSearchMenu.setVisible(false);
            final View searchViewContainer = LayoutInflater.from(actionBar.getThemedContext())
                    .inflate(R.layout.custom_action_bar, null);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            mSearchView = (SearchView) searchViewContainer.findViewById(R.id.search_view);
    
            mSearchView.setIconifiedByDefault(true);
            mSearchView.setQueryHint(getString(R.string.btSearch));
            mSearchView.setIconified(false);
    
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            mSearchView.setOnQueryTextListener(this);
            mSearchView.setOnCloseListener(this);
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    
            actionBar.setCustomView(searchViewContainer,
                    new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                            ActionBar.LayoutParams.WRAP_CONTENT));
            actionBar.setDisplayShowCustomEnabled(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2023-04-10
      • 2017-11-23
      • 2023-03-06
      相关资源
      最近更新 更多