2015-04-01 2 views
2

Мое требование: - The action barВ андроида, как установить вид «кнопок действий» в ActionBar

и когда я нажимаю кнопку поиска Я хочу: - SEarchview Так вот мой код: -

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> 

, но когда я запускаю эту программу, я получил это: - actionbar здесь, то APPNAME закуски не показывает .. и когда я нажимаю поисковое поле, я получил: - searchview

Этот поиск расширен. Где не так в моем коде, что я не могу выполнить свои требования.

ответ

0

Существует метод setMaxWidth (int maxpixels), который вы можете использовать в вашем searchView.

searchView.setMaxWidth(100); 

Try с различными размерами, не уверен, будет достаточно хорош :)

Кроме того, если кнопка контакты не требуется, чтобы быть там все время, которое вы могли бы перейти в XML для

<item android:id="@+id/action_contact" 
android:title="Contacts" 
android:showAsAction="ifRoom"/> 
Смежные вопросы