2017-10-17 15 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 나는 당신이 당신의 목적을 제공하는 Toolbar를 사용하는 것이 좋습니다

enter image description here

+0

네 당신의 로그 캣을 첨부하십시오 당신이 너무 – Mandy8055

+0

Mandy8055 @ 아니,이 오류가 없지만, 옵션 메뉴 내 도구 모음에 표시되지 않는 이유는 무엇입니까? 오류를 얻고있다. 내 일반적인 활동 (활동 대화가 아닌)이 동일한 코드를 사용하고 옵션 메뉴가 표시이기 때문에 어떤 생각이 없어요 –

+0

그 경우에는'optionsMenu'가 표시되어야하는 앱의 스크린 샷을 첨부하십시오 !!! 내가 당신을 도울 수 있도록 도와주세요 ... – Mandy8055

답변

0

을 표시하지 않습니다. 다음은이 당신의 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의에 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"/> 

에 선 아래에 추가하면

도움이 될 일이며, .javaonCreate() 메소드에 아래 라인을 추가합니다.

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

자세한 내용은 this_blog을 참조하십시오.

감사합니다 :)