2015-05-20 2 views
-1

Я пытаюсь добавить кнопку поиска в панель действий в своем приложении, но ничего не отображается в панели действий, отличной от заголовка в окне дизайна Android Studio:Кнопки Android Action не отображаются на панели действий

Example

HomeActivity.java:

public class HomeActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_home, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

menu_home.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context=".HomeActivity"> 
    <item android:id="@+id/action_search" 
     android:title="@string/action_search" 
     android:orderInCategory="1" 
     android:icon="@drawable/ic_action_search" 
     app:showAsAction="always" /> 
    <item android:id="@+id/action_settings" android:title="@string/action_settings" 
     android:orderInCategory="100" app:showAsAction="never" /> 
</menu> 

activity_home.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".HomeActivity"> 
    <TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" /> 

</RelativeLayout> 

Я попытался изменить app:showAsAction к android:showAsAction, но ничего не казалось, чтобы изменить.

+0

Появляется ли оно при запуске приложения? –

+1

@ DanielNugent: О, я попробовал запустить приложение, и появилась кнопка. Думаю, это работает только в реальном приложении, а не в Android Studio. – amalik

ответ

0

Предварительный просмотр показывает, что именно находится в вашем макете, а не что-либо добавленное программно. Если вы хотите просмотреть меню в режиме проектирования, вам нужно сообщить об этом Android Studio:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:menu="menu_home" 
    tools:context=".HomeActivity"> 
+0

Я попробовал ваше предложение, ничего не изменилось в окне дизайна. – amalik

Смежные вопросы