简体   繁体   English

Actionbar按钮未显示在searchview android中

[英]Actionbar button not showing in searchview android

I have a searchview and a button i my actionbar,but the button gets displayed only in the landscape mode and however I try it wont get displayed in the portrait mode. 我有一个搜索视图和一个按钮我的操作栏,但按钮仅在横向模式下显示,但我尝试它不会以纵向模式显示。

Landscape Mode 景观模式

景观模式

Portrait Mode 肖像模式

肖像模式

this is my menu.xml 这是我的menu.xml

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

This is my main activity 这是我的主要活动

public class Myclass extends Activity implements OnQueryTextListener,OnItemClickListener {
GroupAdapter grpAdapter;
public static ArrayList<GroupsModel> arrayOfList;
public static ListView listView;
public static String base_url = "myurl";
private SeekBar volumeControl = null;
private TextView year;
private int progressChanged = 0;
private Menu menu_data;
MenuItem submitBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.two);
    ActionBar actionBar = getActionBar();
    //actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    volumeControl = (SeekBar) findViewById(R.id.volume_bar);
    year = (TextView)findViewById(R.id.year_level);
    volumeControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            progressChanged = progress;
            year.setText(Integer.toString(progressChanged+7));
        }
        public void onStartTrackingTouch(SeekBar seekBar) {

        }
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    arrayOfList = new ArrayList<GroupsModel>();
    listView = (ListView) findViewById(R.id.group_listview);
    listView.setOnItemClickListener(this);
    listView.setTextFilterEnabled(true);

    new ProgressTask(two.this).execute();

}   

private class ProgressTask extends AsyncTask<String, Void, Boolean> { 
    private ProgressDialog dialog;
    @SuppressWarnings("unused")
    private two activity; 
    public ProgressTask(two two) { 
        this.activity = two; 
        context = two; 
        dialog = new ProgressDialog(context);
    }
    private Context context; 
    protected void onPreExecute() { 
        this.dialog.setMessage("Progress start");
        this.dialog.show(); 
    } 
    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        } 
    }
    protected Boolean doInBackground(final String... args) { 
        //arrayOfList = new ArrayList<GroupsModel>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        //params.add(new BasicNameValuePair("",""));
        JSONParser jp = new JSONParser();
        JSONArray groups_obj = jp.makeHttpRequest(base_url + "groups/all", "GET", params);
        for (int i = 0; i < groups_obj.length(); i++) {
            GroupsModel group = new GroupsModel();
            try { 
                JSONObject grp = groups_obj.getJSONObject(i);
                group.setGroupId(grp.getInt("id"));
                group.setGroupname(grp.getString("name"));
                arrayOfList.add(group);
            }
            catch (JSONException e) {
                e.printStackTrace(); 
            }

        }
        two.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                grpAdapter = new GroupAdapter(two.this, R.layout.two_row,arrayOfList);
                listView.setAdapter(grpAdapter);
            }
        });

        return null;
    }
}

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     menu_data = menu;
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
      SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
      searchView.setSubmitButtonEnabled(false);
      searchView.setOnQueryTextListener(this);
      searchView.setQueryHint("My text");
      searchView.setIconifiedByDefault(false);

      submitBtn = (MenuItem)menu_data.findItem(R.id.submit);
      submitBtn.setIcon(R.drawable.button22);
      submitBtn.setEnabled(false);

      return super.onCreateOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     // Handle item selection
     switch (item.getItemId()) {
     case R.id.submit:
         if(submitBtn.isVisible())
         Toast.makeText(getApplicationContext(), Integer.toString(progressChanged+7),Toast.LENGTH_LONG).show();
         return true;
     default:
         return super.onOptionsItemSelected(item);
     }
 }

 @Override
 public boolean onQueryTextChange(String newText)
 {
      // this is your adapter that will be filtered
      if (TextUtils.isEmpty(newText))
      {
            listView.clearTextFilter();
      }
      grpAdapter.getFilter().filter(newText.toString());  
      return true;
 }

 @Override
 public boolean onQueryTextSubmit(String query) {
  // TODO Auto-generated method stub
  return false;
 }

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // TODO Auto-generated method stub
    submitBtn.setIcon(R.drawable.button111);
    submitBtn.setEnabled(true);
    for (GroupsModel s : arrayOfList)
        s.setChecked(false);
    final boolean isChecked = GroupAdapter.items.get(position).getChecked();
    if(isChecked==false){
        GroupAdapter.items.get(position).setChecked(!isChecked);
        grpAdapter.notifyDataSetChanged();
    }
    else{
        GroupAdapter.items.get(position).setChecked(isChecked);
        grpAdapter.notifyDataSetChanged();
    }
}   }

Please help me. 请帮我。

If it doesn't concern you that the SearchView is collapsed at the start, try configuring the SearchView like this: 如果您不关心SearchView在开始时是否已折叠,请尝试像这样配置SearchView:

 android:showAsAction="collapseActionView|ifRoom"

This should make the actionbar show both, the SearchView and your Button. 这应该使动作栏显示,SearchView和你的按钮。 Plus the SearchView will only take up the remaining space to the left, when it gets expanded. 此外,SearchView只会在展开时占用剩余的空间。

According to developer.android.com 根据developer.android.com

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace . 如果您的应用程序使用支持库以获得与Android 2.1一样低的版本的兼容性,那么android:namespace中不提供showAsAction属性 Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. 相反,此属性由支持库提供,您必须定义自己的XML命名空间并将该命名空间用作属性前缀。 (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example: (自定义XML命名空间应该基于您的应用程序名称,但它可以是您想要的任何名称,并且只能在您声明它的文件范围内访问。)例如:

res/menu/main_activity_actions.xml >>>> res / menu / main_activity_actions.xml >>>>

   <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
 </menu>

A SearchView will default to taking up a majority of, if not all of the action bar in portrait mode. SearchView将默认占用纵向模式中的大多数(如果不是全部)操作栏。 Try decreasing the size of the SearchView in menu.xml and see if the button is displayed. 尝试在menu.xml中减小SearchView的大小,看看是否显示了该按钮。

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

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