【问题标题】:Android - Is it possible to change the gravity of a custom dialog?Android - 是否可以更改自定义对话框的重力?
【发布时间】:2013-04-29 15:31:37
【问题描述】:

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

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

有什么方法可以实现吗?我是否必须使用对话框主题创建 Activity?

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

任何帮助将不胜感激!

干杯

【问题讨论】:

  • 你有办法解决这个问题吗?如果有,请发表评论。
  • 我做了,最后还是有不少问题。不过,您的链接帮助我理清了重力,干杯

标签: android android-layout android-activity android-dialog android-dialogfragment


【解决方案1】:

成功了!

我使用 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> 

【讨论】:

    猜你喜欢
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2020-12-12
    • 1970-01-01
    • 2016-10-29
    • 2013-04-20
    相关资源
    最近更新 更多