在我的Galaxy Tab 10.1的日历应用,创建一个新的事件时,一个对话框出现在标题栏/操作栏区完成和取消按钮。
我想在我的应用程序来实现这一点。 我已经尝试使用setHasOptionsMenu(true)
除了重写onCreateOptionsMenu
在我DialogFragment
子类,但我的行动项目不会出现。 我也打过电话getDialog().getActionBar()
从内部onCreateView
但它总是返回null
。
我可以,如果我开始一个得到这个工作Activity
,而不是显示一个对话框,但占据整个屏幕。 有没有做到这一点使用一个标准的方式DialogFragment
?
Answer 1:
使用这个想法从谷歌组后 ,我能够把它关闭造型的活动。 你将要修改的高度和宽度为您选择最好的一个“动态”的大小。 然后设置你喜欢的任何动作条按钮
<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);
}
Answer 2:
如果您正在使用ActionBarSherlock,声明主题如下:
<style name="PopupTheme" parent="Theme.Sherlock">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="windowContentOverlay">@null</item>
</style>
而且,根据初始化PopupTheme一个SherlockActivity 卢克斯利曼的答案 。
private void showAsPopup(SherlockActivity 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); // NO NEED to call this line.
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = activity.getWindow().getAttributes();
params.alpha = 1.0f;
params.dimAmount = 0.5f;
activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// This sets the window size, while working around the IllegalStateException thrown by ActionBarView
activity.getWindow().setLayout(width,height);
}
结果:
Answer 3:
遇到了一些麻烦实施从StrikeForceZero和卢克斯利曼建议的解决方案,所以我想贡献我的经验。 我敢肯定有只是我丢失的东西所以反馈将不胜感激。
我所做的是以下几点:
创建使用提供PopupTheme,直接复制/粘贴样式:
<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>
添加showAsPopup()方法,在片段的方法,这将打开对话框假片段,直复制/粘贴:
private 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.alpha = 1.0f; params.dimAmount = 0f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // This sets the window size, while working around the IllegalStateException thrown by ActionBarView activity.getWindow().setLayout(850,850); }
创建使用简单的new()调用一个新的活动的实例,然后将它传递给showAsPopup()方法:
DialogTestActivity test = new DialogTestActivity(); showAsPopup(test);
对于测试的目的(我只是想确认我可以打开被描述成一个操作栏的对话活动),我用了一个非常简单的测试,直接从按钮视图API演示被盗(用于布局文件见buttons_1.xml在API演示):
public class DialogTestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.buttons_test); } }
不幸的是,每次我想这个时候,我得到的第一个电话,activity.requestWindowFeature(Window.FEATURE_ACTION_BAR)未指定的空指针异常;
04-29 16:39:05.361: W/System.err(15134): java.lang.NullPointerException
04-29 16:39:05.361: W/System.err(15134): at android.app.Activity.requestWindowFeature(Activity.java:3244)
04-29 16:39:05.371: W/System.err(15134): at packagenameremovedforlegalreasons.classname.showAsPopup(classname.java:602)
04-29 16:39:05.371: W/System.err(15134): at packagenameremovedforlegalreasons.classname.onMapLongClick(classname.java:595)
04-29 16:39:05.371: W/System.err(15134): at com.google.android.gms.maps.GoogleMap$5.onMapLongClick(Unknown Source)
04-29 16:39:05.371: W/System.err(15134): at com.google.android.gms.internal.k$a.onTransact(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at android.os.Binder.transact(Binder.java:310)
04-29 16:39:05.381: W/System.err(15134): at com.google.android.gms.maps.internal.IOnMapLongClickListener$Stub$Proxy.onMapLongClick(IOnMapLongClickListener.java:93)
04-29 16:39:05.381: W/System.err(15134): at maps.i.s.a(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.y.v.d(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.y.bf.onLongPress(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.d.v.onLongPress(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.d.h.c(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.d.h.c(Unknown Source)
04-29 16:39:05.381: W/System.err(15134): at maps.d.j.handleMessage(Unknown Source)
04-29 16:39:05.391: W/System.err(15134): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 16:39:05.391: W/System.err(15134): at android.os.Looper.loop(Looper.java:137)
04-29 16:39:05.391: W/System.err(15134): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-29 16:39:05.391: W/System.err(15134): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 16:39:05.391: W/System.err(15134): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 16:39:05.391: W/System.err(15134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-29 16:39:05.391: W/System.err(15134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-29 16:39:05.391: W/System.err(15134): at dalvik.system.NativeStart.main(Native Method)
正如你可以从堆栈跟踪看,预期的行为是在打开的窗口上长按上GoogleMap的实例(使用API 2 MapFragments)。 所以,我首先想到的是,有试图从一个片段打开一个问题,所以我通过电话回所属活动。 同样的错误,同样没有附加信息。
在这一点上我最好的猜测是,一个新的()调用没有充分实例化类/视图,以拨打电话修改其观点。 原来,这似乎是至少在某种程度上真的如视图修改代码迁移到活动,只需打开活动以正常的方式工作:
调用活动:
public void openMapDialog()
{
Intent intent = new Intent(this, DialogTestActivity.class);
startActivity(intent);
}
新类代码:
public class DialogTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// From: https://stackoverflow.com/questions/11425020/actionbar-in-a-dialogfragment
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = this.getWindow().getAttributes();
params.alpha = 1.0f;
params.dimAmount = 0f;
this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// This sets the window size, while working around the IllegalStateException thrown by ActionBarView
this.getWindow().setLayout(600,600);
setContentView(R.layout.buttons_test);
}
}
所以我想我张贴这一切的问题是要弄清楚,如果你想要做什么上面的海报建议,你不能只是新的()的活动,并呼吁showAsPopup()。 这可能是我缺乏经验与Android显示了通过,但同时这似乎有点明显,这也很自然地解释showAsPopup()作为当前视图,而不是观点被称为被创建,因为你正在传递活动实例(这仅仅是这一点 ,如果它应该在的onCreate()就像我终于实现了做)。
所以,如果目的是要调用showAsPopup()的创建活动,而不是创建的活动,这不是明显的如何得到活动实例,它是之前的onCreate修改()被调用。 是,你不能把喜欢的东西后的setContentView requestWindowFeature()()的问题称为( 例如 ),这是一个问题,因为它通常被称为的onCreate()。
同样,如果有一个简单/更好的方式来做到这一点,我将非常感谢反馈。 但愿这是谁想要使用这种方法的人很有帮助。
Answer 4:
我已经花了许多时间玩这个周围难以置信的。 接受答案的工作在Galaxy Nexus 7(安卓4.2),但未能在三星Galaxy SIII(安卓4.1)和三星Galaxy Tab 10.2(Android 4.0版本),但下列情况除外:
IllegalStateException: ActionBarView can only be used with android:layout_width="match_parent" (or fill_parent)
这是通过在代码引起ActionBarView.onMeasure(INT,INT) ,其检查,以确保布局被设置为match_parent。 正确的溶液经由setLayout的代替设置窗口的宽度,而不是使用setAttributes的。
这是showAsPopup(),这对我已经下测试所有设备工作的一个固定的版本:
private 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.alpha = 1.0f;
params.dimAmount = 0f;
activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// This sets the window size, while working around the IllegalStateException thrown by ActionBarView
activity.getWindow().setLayout(850,850);
}
为了完整起见,这里是PopupTheme再次:
<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>
Answer 5:
像Veeti暗示,你可能想尝试用对话的主题实施的活动。 在Android清单:
<activity android:name=".YourActivity" android:theme="@android:style/Theme.Dialog </activity>
希望这会有所帮助。
Answer 6:
我可能不是在有经验的,但我会用一个活动,添加操作栏项目,然后:
<activity android:name=".YourActivity"
android:theme="@android:style/Theme.Holo.Light.Dialog" />
希望帮助!
Answer 7:
我找到了一个很好的方式做到这一点,利用setCustomTitle在alertDialog的DialogFragment(也有可能在alertDialog本身)的建设者。
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity,...);
final Toolbar toolbar = (Toolbar) inflater.inflate(R.layout.dialog_app_filter_toolbar, null, false);
// <= prepare toolbar and dialog here
return builder.create();
}
而结果(从我的应用程序 ):
如果你不希望使用AlertDialog,你仍然可以只是把一个工具栏,你的对话框的布局和使用它。
Answer 8:
我知道这是不是真正的答案,但上述方案都显得很不雅观一个可能会认为,这将是更容易避免操作栏可言,像一些与.noActionBar继承你的对话主题,然后在你的面板顶部创建视图模仿行动起来吧,至少这样它在XLM所有入住。
文章来源: ActionBar in a DialogFragment