gpt4 book ai didi

android - 操作栏按钮未显示在 searchview android 中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:08 26 4
gpt4 key购买 nike

我的操作栏上有一个搜索 View 和一个按钮,但该按钮仅在横向模式下显示,但我尝试过它不会在纵向模式下显示。

横向模式

Landscape Mode

人像模式

Portrait Mode

这是我的 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>

这是我的主要 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();
}
} }

请帮帮我。

最佳答案

如果您不关心 SearchView 在开始时折叠,请尝试像这样配置 SearchView:

 android:showAsAction="collapseActionView|ifRoom"

这应该使操作栏同时显示 SearchView 和您的 Button。另外,SearchView 在扩展时只会占用左侧的剩余空间。

关于android - 操作栏按钮未显示在 searchview android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24118333/

26 4 0