代码之家  ›  专栏  ›  技术社区  ›  MrX

在“活动”对话框上添加选项菜单

  •  0
  • MrX  · 技术社区  · 7 年前

    我需要添加一个 Option Menu 或“我的活动”对话框上的图标。这个 选项菜单 没有显示。我使用自定义主题创建自定义活动对话框。我也使用了自定义标题。下面是我的代码片段,以明确说明:

    显示xml

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

    <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>
    

    模态活动。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);
    }
    

    编辑

    在图片下方,我需要添加一些 选项菜单 在我的标题中,但是 选项菜单 未显示

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  1
  •   user7571182 user7571182    7 年前

    我建议您使用 Toolbar

    在中添加以下行 布局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"/>
    

    这是给你的 样式。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>
    

    模态活动。Java语言 在中添加以下行 onCreate() 方法:

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

    您也可以参考 this_blog

    问候:)