我的操作栏上有一个搜索视图和一个按钮,但该按钮仅在横向模式下显示,但我尝试它不会以纵向模式显示。
横向模式
人像模式
这是我的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>
这是我的主要活动
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();
}
} }
请帮帮我。
答案 0 :(得分:3)
如果您不关心SearchView在开始时是否已折叠,请尝试像这样配置SearchView:
android:showAsAction="collapseActionView|ifRoom"
这应该使动作栏显示,SearchView和你的按钮。此外,SearchView只会在展开时占用剩余的空间。
答案 1 :(得分:1)
如果您的应用使用支持库以获得与Android 2.1一样低的版本的兼容性,那么 showAsAction属性不能从android:namespace 获得。相反,此属性由支持库提供,您必须定义自己的XML命名空间并将该命名空间用作属性前缀。 (自定义XML命名空间应该基于您的应用程序名称,但它可以是您想要的任何名称,并且只能在您声明它的文件范围内访问。)例如:
res / menu / main_activity_actions.xml&gt;&gt;&gt;&gt;
<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>
答案 2 :(得分:1)
SearchView将默认占用纵向模式中的大多数(如果不是全部)操作栏。尝试在menu.xml中减小SearchView的大小,看看是否显示了该按钮。