我的要求是: -
当我点击我想要的搜索按钮时: - 所以这是我的代码: -
Snacks.java
public class Snacks extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snacks);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
getActionBar().setTitle("Snacks");
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconified(true);
int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchImg = (ImageView) searchView.findViewById(searchImgId);
searchImg.setImageResource(R.drawable.ic_action_search);
int searchDeleteImgId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView searchDeleteImg = (ImageView) searchView.findViewById(searchDeleteImgId);
searchDeleteImg.setImageResource(R.drawable.ic_action_delete);
int searchSrcTextId = getResources().getIdentifier( "android:id/search_src_text", null, null);
EditText searchEditText = (EditText) searchView.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.WHITE);
searchEditText.setHintTextColor(Color.LTGRAY);
searchEditText.setHint("Search Here");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Toast.makeText(getApplicationContext(), "Entered Keyword is " + s,Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
}
activity_main_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
android:title="Search"/>
<item android:id="@+id/action_contact"
android:title="Contacts"
android:showAsAction="always"/>
<item android:id="@+id/settings"
android:title="Contacts"
android:showAsAction="never"/>
</menu>
styles.xml
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
但是当我运行这个程序时,我得到了这个: - 在这里,appname Snacks 没有显示..当我点击我的搜索视图时: -
此搜索视图全面展开。我的代码哪里出错我无法达到我的要求..
答案 0 :(得分:0)
您可以在searchView上使用setMaxWidth(int maxpixels)方法。
searchView.setMaxWidth(100);
尝试不同尺寸,不确定哪个尺寸足够好:)。
此外,如果联系人按钮不是必须一直在那里,你可以切换到xml
<item android:id="@+id/action_contact"
android:title="Contacts"
android:showAsAction="ifRoom"/>