【问题标题】:Android AlertDialog with custom width具有自定义宽度的 Android AlertDialog
【发布时间】:2012-10-29 05:12:18
【问题描述】:

我知道这个问题的答案一定在某个地方,但我一直找不到...

我有一个自定义的 AlertDialog,无论我在 XML 文件和/或 Java 代码中设置什么参数,对话框总是以全屏宽度显示。

现在,我的布局文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_picker_dlg_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TableRow>
[...]
</TableRow>
</TableLayout>

然后我用这样的代码创建 AlertDialog:

layout = inflater.inflate(R.layout.color_picker_dlg, (ViewGroup) findViewById(R.id.color_picker_dlg_root));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
[...]
dialog = builder.create();

...对话框仍然占据了所有的屏幕宽度。

我能做些什么来让它包裹它的内容?

提前致谢。

【问题讨论】:

    标签: android dialog width


    【解决方案1】:

    我的对话框也有同样的问题。为了治愈它,我必须使用对话主题设置我的对话。不知道如何使用警报对话框来做到这一点,但这是常规对话框的代码。

        dialog = new Dialog(context, R.style.CustomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_custom_blank);
        dialog.setCancelable(isCancelable);
    

    这里是 CustomDialog 主题

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CustomDialog" parent="android:Theme.Dialog">
    
          <item name="android:layout_width">wrap_content</item>
          <item name="android:layout_height">wrap_content</item>
          <item name="android:windowIsFloating">true</item>
          <item name="android:windowNoTitle">true</item>
    
        </style>
    
    </resources>
    

    如果您真的想进入对话框并创建自己的自定义可重用对话框,请查看我网站上的教程

    http://www.androidianlabs.com/custom-android-dialogs.html

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多