本文介绍了不能看到动作栏搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知怎的,我无法看到的动作栏搜索。

菜单主:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    工具:上下文=com.example.actionbar.MainActivity>

    <项目机器人:ID =@ + ID / action_settings
        机器人:标题=@字符串/ action_settings
        机器人:orderInCategory =100
        应用程序:showAsAction =从不/>
    <项目机器人:ID =@ + ID / ACTION_SEARCH
        机器人:标题=搜索
        机器人:图标=@机器人:可绘制/ ic_menu_search
        机器人:showAsAction =collapseActionView | ifRoom
        机器人:actionViewClass =android.widget.SearchView
        >

    < /项目>
< /菜单>
 

MainActivity

 公共类MainActivity扩展ActionBarActivity {

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

        如果(savedInstanceState == NULL){
            getSupportFragmentManager()的BeginTransaction()
                    。新增(R.id.container,新PlaceholderFragment())
                    。承诺();
        }
    }


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

        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。将操作栏
        //自动在主/向上按钮操作的点击,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT的id = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }

    / **
     *包含一个简单的视图的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{

        公共PlaceholderFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
            返回rootView;
        }
    }

}
 

解决方案

试试这个(其他城市的android:showAsAction到应用程序:showAsAction):

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
工具:上下文=com.example.actionbar.MainActivity>

<项目机器人:ID =@ + ID / action_settings
    机器人:标题=@字符串/ action_settings
    机器人:orderInCategory =100
    应用程序:showAsAction =从不/>
<项目机器人:ID =@ + ID / ACTION_SEARCH
    机器人:标题=搜索
    机器人:图标=@机器人:可绘制/ ic_menu_search
    应用程序:showAsAction =collapseActionView | ifRoom
    机器人:actionViewClass =android.widget.SearchView
    >

< /项目>
 

和您可以强制出现它是:

 安卓showAsAction =总是
 

Somehow I am unable to see search in action bar.

menu main:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.actionbar.MainActivity" >

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
    <item android:id="@+id/action_search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView"
        >

    </item>
</menu>

MainActivity

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}
解决方案

Try this (change android:showAsAction to app:showAsAction) :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.actionbar.MainActivity" >

<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView"
    >

</item>

And you can force-show it by:

android:showAsAction="always"

这篇关于不能看到动作栏搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 01:39