要在 Dialog 中使用操作栏,您必须在 style.xml 中创建一个自定义主题,其父主题为“Theme.AppCompat.Light”。
<style name="PopupTheme" parent="Theme.AppCompat.Light">
<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>
</style>
并在您的清单中使用活动标签添加此样式:
<activity
android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|locale"
android:screenOrientation="portrait"
android:theme="@style/PopupTheme" >
最后在您的活动中添加此代码setConytentView(layoutID)
;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
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 = 0.5f;
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,900);
setContentView(R.layout.dialog_move);
}