【问题标题】:How to add margin right and left of dialog如何在对话框的左右添加边距
【发布时间】:2017-02-25 04:00:30
【问题描述】:

我希望对话框不会拉伸以适应显示屏的宽度。我希望对话框有大约 90% 的屏幕宽度并将其居中。 我该怎么做?

非常感谢^^

【问题讨论】:

  • 我们可以看看你试图打开对话框的代码吗?

标签: android android-layout dialog


【解决方案1】:

您可以使用以下属性指定样式以获取屏幕百分比的对话框大小。

<style name="YourDialogTheme">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>

然后在对话框中应用如下主题:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.YourDialogTheme));

windowMinWidthMinorwindowMinWidthMajor

【讨论】:

  • 很高兴知道。您也可以接受答案,因为它适合您。
【解决方案2】:

对于最简单的方法,您可以执行以下操作:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/defaultMargin"
android:paddingRight="@dimen/defaultMargin">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</RelativeLayout>

另外,这个link可以参考

【讨论】:

    【解决方案3】:

    这是在对话框中设置边距的示例

    final Dialog dialog = new Dialog(context);
    // ... 
    
    // e.g. top + right margins: 
    dialog.getWindow().setGravity(Gravity.TOP|Gravity.RIGHT);
    WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
    layoutParams.x = 100; // right margin
    layoutParams.y = 170; // top margin
    dialog.getWindow().setAttributes(layoutParams);
    
    // e.g. bottom + left margins: 
    dialog.getWindow().setGravity(Gravity.BOTTOM|Gravity.LEFT);
    WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
    layoutParams.x = 100; // left margin
    layoutParams.y = 170; // bottom margin
    dialog.getWindow().setAttributes(layoutParams);
    

    【讨论】:

    • 它对我不起作用。我之前尝试过,如果我只想为左右设置布局边距。我必须设置什么重力?我应该在 dialog.show() 和 dialog.setContentview() 函数之前或之后使用这段代码
    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2018-11-30
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多