2015-11-21 5 views
0

Я пытаюсь добавить меню переполнения (три точки) в свое приложение для Android. Он отлично выглядит на предварительном просмотре (Android Studio), но не отображается на моем планшете Samsung Galaxy Tab 2. Зачем?Меню Android Overflow прекрасно работает в Android Studio, но не отображается на самом устройстве

Вот что я сделал до сих пор:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.boci.helloworldandroidgithub" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.ActionBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="ThemeOverlay.AppCompat.ActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
    <style name="AppTheme.ActionBar"> 
     <item name="windowActionBar">true</item> 
     <item name="windowNoTitle">false</item> 
    </style> 
    <style name="AppTheme.AppBarOverlay" parent="Base.ThemeOverlay.AppCompat.ActionBar" /> 
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.ActionBar" /> 

</resources> 

styles.xml (v21)

<resources>> 
    <style name="AppTheme.ActionBar"> 
     <item name="windowActionBar">true</item> 
     <item name="windowNoTitle">false</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/holo_blue_light</item> 
    </style> 
</resources> 

MainActivity.java

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 

    return super.onCreateOptionsMenu(menu); 
} 

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> 

    <group android:checkableBehavior="single" android:enabled="true" > 

     <item android:id="@+id/main_red" 
      android:orderInCategory="1" 
      app:showAsAction="never" 
      android:title="@string/menu_red"> 
     </item> 

     <item android:id="@+id/main_green" 
      android:orderInCategory="2" 
      app:showAsAction="never" 
      android:title="@string/menu_green"> 
     </item> 

     <item android:id="@+id/main_blue" 
      android:orderInCategory="3" 
      app:showAsAction="never" 
      android:title="@string/menu_blue"> 
     </item> 

    </group> 

</menu> 

Что еще нужно настроить, если я хотел бы видеть эти три точки и варианты?

ответ

0

Кнопка переполнения не показывается из-за этого кода в ActionBarPolicy:

public boolean showsOverflowMenuButton() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     return true; 
    } else { 
     return !ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(mContext)); 
    } 
} 

Как вы можете видеть, если устройство было меньше KitKat кнопка переполнения не отображается, если устройство имеет аппаратный ключ.

Вы можете изменить эти настройки, добавив этот код в onCreate методы вашего Activity:

try { 
    ViewConfiguration config = ViewConfiguration.get(this); 
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); 
    if (menuKeyField != null) { 
     menuKeyField.setAccessible(true); 
     menuKeyField.setBoolean(config, false); 
    } 
} catch (Exception ex) { 
} 
+0

Это звучит вполне логично, и я пустошь любви, чтобы принять в качестве правильного ответа. Единственная проблема, однако, что я все еще не могу увидеть эти три кровавые точки :-((или что-нибудь на панели действий) –