2016-01-10 107 views
1

我是新來的android應用程序,我希望有一個敏銳的眼睛可以發現我做錯了什麼。我試着按照步驟here,爲我的應用程序創建一個工具欄。但是,它沒有顯示三點溢出菜單(我認爲這是自動的)。Android工具欄不顯示溢出菜單

這是我看到:

enter image description here

我使用的minSdkVersion 19

MainAcivitiy.java

import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 


public class MainActivity extends AppCompatActivity { 

@SuppressLint("ShowToast") 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(myToolbar); 

佈局\ main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:descendantFocusability="beforeDescendants" 
android:focusableInTouchMode="true"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

菜單\ main.xml中

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

<item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="@string/action_settings" 
    app:showAsAction="never" 
    android:visible="true" /> 

styles.xml

<resources> 
<!-- 
    Base application theme, dependent on API level. This theme is replaced 
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
--> 
<style name="AppTheme.Base" parent="Theme.AppCompat.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 

<style name="AppBaseTheme" parent="Theme.AppCompat.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> 

回答

9

我想你錯過了這個

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
+1

謝謝。這爲我解決了這個問題。 – shadsnz

+4

即使到2016年年底,這個基本說明也不在培訓文檔中。也許需要更新? –

0

如果你的手機有物理菜單按鈕,你不能看到三個點

如果你想強制顯示三個點如下:

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

<item 
    android:id="@+id/menu" 
    app:showAsAction="always" 
    android:icon="@drawable/ic_action_overflow" 
    android:visible="true" > 
<menu> 
    <item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="@string/action_settings" 
    app:showAsAction="never" 
    android:visible="true" /> 
</menu> 
</item> 
1

解決方案:我已嘗試以下方法,我已經找到了問題。

我認爲這是指該主題Theme.AppCompat.Light這就是爲什麼這個主題有一個工具欄。

我只是做這個和它的工作:

<resources> 
    <!-- 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
    --> 
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="android:windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
    </style> 

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- 
      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> 

結果:

enter image description here

它的工作。 但是,

有了您的代碼和getSupportActionBar();

enter image description here

請注意,我們的工具欄上有一個灰色像這樣:

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@android:color/darker_gray" 
      android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

PS:此外,我想請確保你完全做到這一點,正如文件所述,我們必須將此添加到Manifest

<application 
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
    /> 

和好像它不工作,或者我們已經失去了something.BTW,我沒有這樣做,我的表現並沒有固定在清單上面的代碼。

0

我有同樣的問題。我在這裏嘗試了一切,here,沒有什麼幫助。一切都像是應該的。調查的一整天后,我發現這種說法在我的onCreate方法:

toolbar.setVisibility(View.INVISIBLE); 

當我刪除它,geuss發生了什麼:3點菜單現在看來,就像它應該。

相關問題