2017-10-17 42 views
1

請在將此標記爲「重複」之前閱讀!在活動對話框中添加OptionMenu

我需要在我的活動對話框中添加一個Option Menu或圖標,但我在這裏呆了兩天。 Option Menu沒有顯示。我使用自定義主題來製作自定義活動對話框。我也使用了一個自定義標題。在這裏我的代碼片段,以明確:

的manifest.xml

<activity 
     android:name=".ModalActivity" 
     android:theme="@style/Theme.Custom.AlertDialog" /> 

style.xml

<style name="Theme.Custom.AlertDialog" parent="Theme.AppCompat"> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowNoTitle">false</item> 
</style> 

ModalActivity.java

public class ModalActivity extends AppCompatActivity implements View.OnClickListener{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(getWindow().FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.modal_activity); 

    DisplayMetrics metrics = getResources().getDisplayMetrics(); 
    int screenWidth = (int) (metrics.widthPixels * 0.90); 

    getWindow().setLayout(screenWidth, ViewGroup.LayoutParams.WRAP_CONTENT); 
    getWindow().setBackgroundDrawableResource(R.color.colorRedTheme); 
    this.setFinishOnTouchOutside(false); 
    getWindow().setFeatureInt(getWindow().FEATURE_CUSTOM_TITLE, R.layout.title_dialog_editor); 

    final TextView customTitle = (TextView) findViewById(R.id.title_text_dialog_editor); 
    if (customTitle != null) { 
     customTitle.setText("Modal Activity"); 
     customTitle.setTextSize(20f); 
     customTitle.setPadding(5, 5, 5, 0); 
     customTitle.setBackgroundColor(Color.parseColor("#BA0B0B")); 
     customTitle.setTextColor(Color.WHITE); 
    } 
    setTitle(""); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.refresh: 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

的問題是菜單不顯示。我在這裏搜索關於我的案例,但沒有解決方案。任何建議和幫助將感謝。

編輯: 下面的圖片中,我需要補充一些Option Menu在我的標題,但Option Menu不表明

enter image description here

+0

你得到任何錯誤?如果是,請附上您的logcat太 – Mandy8055

+0

@ Mandy8055沒有,沒有錯誤,但在選項菜單中是不是在我的工具欄顯示。我沒有任何想法,因爲我的正常活動(不是活動對話框)使用相同的代碼和選項菜單是顯示 –

+0

在這種情況下,請您附上'optionsMenu'需要顯示的應用程序的屏幕截圖!!!它會幫我幫助你... – Mandy8055

回答

0

我建議你使用Toolbar服務於你的目的。下面是一些可以幫助你

添加以下線路在Layout.xml

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="?attr/colorPrimary" 
    android:elevation="8dp" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

這對您的styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="Theme.Custom.AlertDialog" parent="Theme.AppCompat""> 
    <!-- Customize your theme here. --> 
    <!-- your app branding color for the app bar --> 
    <item name="colorPrimary">#3F51B5</item> 
    <!-- darker variant for the status bar and contextual app bars --> 
    <item name="colorPrimaryDark">#303F9F</item> 
    <!-- theme UI controls like checkboxes and text fields --> 
    <item name="colorAccent">#FF4081</item> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowNoTitle">false</item> 

</style> 

</resources> 

最後,在你的ModalActivity .java在您的onCreate()方法中加入以下幾行:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

您也可以參考this_blog尋求更多幫助和支持。

問候:)