0

下面的图像基本上是我想要实现的,这直接来自 eclipse 作为我的 dialog_layout.xml

我想膨胀一个自定义对话框并将其放在屏幕右侧作为菜单。每次,我都尝试展示这一点DialogFragment,下面的布局以屏幕中心为中心。(onCreateDialog()`DialogFragment1 的代码在图像下方。

有什么办法可以做到这一点?我是否必须使用对话框主题创建活动?

所需的对话框

@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();
}

任何帮助,将不胜感激!

干杯

4

1 回答 1

2

成功!

我使用 DialogFragment 在屏幕右上角生成自定义菜单对话框。

在此处输入图像描述

推理:

  • 需要大按钮,因此标准菜单太小。
  • 标准菜单是从自定义菜单按钮膨胀的麻烦。
  • 我希望右上角有一个相当窄的对话框,与用户按下菜单按钮时的拇指位置相同。这与大多数菜单的逻辑相同。

面临的问题:

  • 我的对话框周围不需要的黑色边框|| 解决方案: dialog.setView(dialogLayout, 0, 0, 0, 0);
  • 对话框始终居中 || 解决方案:更改了 WindowParams。
  • 尽管一开始就可以看到 XML 设计,但对话框占据了屏幕的大部分 || 解决方案:应用自定义透明窗口背景主题

代码:

@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> 
于 2013-05-01T09:46:14.047 回答