DialogFragment 中的 ActionBar [英] ActionBar in a DialogFragment

查看:31
本文介绍了DialogFragment 中的 ActionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Galaxy Tab 10.1 上的日历应用程序中,创建新事件时会出现一个对话框,标题栏/操作栏区域中会出现完成"和取消"按钮.

In the Calendar app on my Galaxy Tab 10.1, when creating a new event a dialog comes up with Done and Cancel buttons in the title bar/action bar area.

我想在我的应用中实现这一点.除了在我的 DialogFragment 子类中覆盖 onCreateOptionsMenu 之外,我还尝试使用 setHasOptionsMenu(true),但我的操作项没有出现.我也试过从 onCreateView 中调用 getDialog().getActionBar() 但它总是返回 null.

I'd like to implement this in my app. I've tried using setHasOptionsMenu(true) in addition to overriding onCreateOptionsMenu in my DialogFragment subclass, but my action items do not appear. I've also tried calling getDialog().getActionBar() from within onCreateView but it always returns null.

如果我启动一个 Activity 而不是显示一个对话框,但它占据了整个屏幕,我就能让它工作.是否有使用 DialogFragment 执行此操作的标准方法?

I am able to get this working if I start an Activity rather than showing a dialog but that takes up the whole screen. Is there a standard way to do this using a DialogFragment?

推荐答案

使用来自 google group post 我能够将它拉下来设计一个活动.您最好将高度和宽度修改为您选择的动态"尺寸.然后设置任何你想要的 ActionBar 按钮

using the idea from a google group post I was able to pull it off styling an activity. you would want to modify the height and width to a "dynamic" size of your choice preferably. Then set whatever ActionBar buttons you would like

<style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

--

public static void showAsPopup(Activity activity) {
    //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
    activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    LayoutParams params = activity.getWindow().getAttributes(); 
    params.height = 850; //fixed height
    params.width = 850; //fixed width
    params.alpha = 1.0f;
    params.dimAmount = 0.5f;
    activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    setContentView(R.layout.activity_main);
}

这篇关于DialogFragment 中的 ActionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆