下面的图片基本上就是我想要实现的,这就是我的dialog_layout.xml直接来自eclipse
我想给自定义对话框充气,并将其放在屏幕右侧作为菜单。每次,我都会尝试显示此DialogFragment
,下面的布局将自身置于屏幕中心。 (DialogFragment1的onCreateDialog()
的代码位于图像下方。
有没有办法实现这个目标?我是否必须使用对话框主题创建活动?
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Create a new Dialog using the AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View background = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(background);
return builder.create();
}
任何帮助将不胜感激!
干杯
答案 0 :(得分:2)
成功!
我使用DialogFragment在屏幕右上角生成一个自定义菜单对话框。
推理:
面临的问题:
代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Create a new Dialog and apply a transparent window background style
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.dialog_theme);
AlertDialog dialog = builder.create();
// Create the custom dialog layout and set view with initial spacing parameters to prevent black background
View dialogLayout = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment_menu, null);
dialog.setView(dialogLayout, 0, 0, 0, 0);
// Change the standard gravity of the dialog to Top | Right.
WindowManager.LayoutParams wlmp = dialog.getWindow().getAttributes();
wlmp.gravity = Gravity.TOP | Gravity.RIGHT;
return dialog;
}
我的风格代码很简单:
<style name="dialog_theme" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>