自定义对话框过小。

23

我有一个实现了自定义对话框的Android活动。应用程序正常运行,但是对话框太小了,我想显示一个更大的对话框。我该如何实现这个目标?以下是我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

         <LinearLayout 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:background="@drawable/view_more_borders">

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:orientation="horizontal"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

             <TextView
                 android:id="@+id/share_amount"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="Company Name:"
                 android:textAppearance="?android:attr/textAppearanceSmall"
                 android:textColor="@color/nice_blue"
                 android:typeface="sans" />

            <TextView
                android:id="@+id/textViewCompanyName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.80"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />

         </LinearLayout>


         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

             <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Price per share:"

                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/nice_blue" />

            <TextView
                android:id="@+id/textViewprice_pershare"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:layout_alignBaseline="@+id/Yesterday"
                android:layout_alignBottom="@+id/Yesterday"
                android:layout_marginLeft="38dp"
                android:fontFamily="sans-serif"
                android:layout_toRightOf="@+id/company"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

              <TextView
                    android:id="@+id/textView4"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Total cost:"

                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:textColor="@color/nice_blue" />

            <TextView
                android:id="@+id/textview_totalcost"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:layout_alignBaseline="@+id/Yesterday"
                android:layout_alignBottom="@+id/Yesterday"
                android:layout_marginLeft="38dp"
                android:fontFamily="sans-serif"
                android:layout_toRightOf="@+id/company"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>


         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">

                 <TextView
            android:id="@+id/textView3"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Number of shares:"

                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="@color/nice_blue" />

            <EditText
                android:id="@+id/shareNumber"
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:fontFamily="sans-serif"
                android:textSize="12sp"
                 >

            <requestFocus />
            </EditText>
         </LinearLayout>

         <LinearLayout 
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_weight="1"
             android:layout_marginTop="4dp"
             android:gravity="center_vertical">


              <TextView
        android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Payment method:"

                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/nice_blue" />

             <Spinner
                 android:id="@+id/spinner_paymentmode"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="2" />

         </LinearLayout>   
     </LinearLayout>
     <Button
    android:id="@+id/button_buy_shares"
    style="@style/ButtonText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/blue_button"
    android:text="Buy" />

以下是我的输出结果。我想增加对话框的宽度和高度。 enter image description here

这是我的Java文件。

public class MyDialogFragment extends DialogFragment {

public MyDialogFragment() {

}

private EditText mEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialogfragment, container);
    // mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    // getDialog().setTitle("Hello");
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

    return view;
}
}

发布你的Java文件。你必须在Java文件中设置对话框的宽度和高度。 - Sanket Shah
@Andrain,我已经发布了它。 - mungaih pk
@mungaihkamau,你解决了你的问题吗? - JJ86
@JaAd 不,我还在尝试从谷歌上找到一些东西。 - mungaih pk
@mungaihkamau,请再试一次我的代码片段。 - JJ86
将RelativeLayout添加为自定义对话框布局的根元素,这对我起作用了! - beginner
11个回答

58

在运行时更改您的对话框尺寸:

yourDialog.show();
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);

你可以对两个维度都这样做,在我的例子中,我只改变了宽度。希望能帮到你!

编辑

我忘记提到我从哪里获取了宽度:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

编辑2

尝试这段代码:

Dialog yourDialog = dialogFragment.getDialog();
yourDialog.getWindow().setLayout((6 * width)/7, (4 * height)/5);

你是如何初始化宽度的? - mungaih pk
1
@Chris 这段代码的作用是让对话框占据屏幕宽度的6/7和屏幕高度的4/5。 - JJ86
但如果我将宽度设置为match_parent,问题仍然存在吗? - Parth Anjaria
@ParthAnjaria 抱歉,我没有注意到你的评论。我不知道,但也许现在不允许使用此参数进行对话了。我认为即使这很有用,拥有一个大对话框也不是一个好的解决方案(设计)。即使我的解决方案有效,也许现在已经不完美了。如果您需要显示大量信息,请重新考虑并使用Fragment或Activity。 - JJ86
2
就是这样。谢谢!我之前使用了 getActivity().getWindow() 而不是 getDialog().getWindow(),请注意这必须在 UI 准备好并且不是在 onCreate() 中调用,而是在 onStart() 中调用。 - Alan Deep
只是额外的信息,如果您想要高度自适应内容,可以使用 dialog.getWindow().setLayout(yourWidth, WindowManager.LayoutParams.WRAP_CONTENT) - adrilz

20

在自定义布局中设置android:minWidth和android:minHeight可以解决问题。


9

将以下代码行放入onCreate函数中

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);

original answer


5
是的,它可以工作,但是1)对话框扩展到整个宽度,2)此主题已过时,您可以使用 Theme_DeviceDefault_Dialog_MinWidthTheme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth 或任何 Material 主题。在布局中设置 styleandroid:theme 也无法解决问题,所以我们应该使用 setStyle() 方法。 - CoolMind

6

对话框的大小由其内容确定。您可以在外部布局上添加填充和边距以占用更多空间,但这不会重新分配内部视图。


1
这是最好的解释!在自定义布局中,您不能使用match_parent,因为Dialog本身始终使用wrap_content。 - Evgenii Vorobei

6
<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:minWidth" type="dimen">600dp</item>
</style>

style.xml文件中使用此代码,并在对话框类中使用此样式。


2
在这里对我有效的是minWidth=600dp。在我的情况下,我不需要其他样式元素。 - luckyhandler

3

在您的根视图中添加 android:minWidth/android:minHeight。以下是示例:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="300dp"
android:minHeight="500dp">

2

如果DialogFragment不使用AlertDialog.Builder,它将无法起到作用。 - CoolMind

1

这个技巧对我起作用了

  override fun onStart() {
        super.onStart()
        val metrics: DisplayMetrics = resources.displayMetrics
        val width: Int = metrics.widthPixels
        dialog?.window?.setLayout(width - 58, 
        ViewGroup.LayoutParams.WRAP_CONTENT)
    }

0
尝试为基本的LinearLayout设置android:layout_height的值。
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

<!-- right here -->
    android:layout_height="100dp"

android:background="@color/white"
android:orientation="vertical" >

0

我配置了我的项目,使得DialogFragments的默认对话框主题具有最小宽度。

AndroidManifest.xml中将应用程序的主题设置为自定义主题... 或者将托管DialogFragmentActivity的主题设置为您的应用程序的自定义主题。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">     <!-- set the app's theme to your custom theme -->

        .....

    </application>

</manifest>

values/styles.xml中定义您的自定义主题,并使用您自己定义的主题覆盖用于对话框片段的默认主题。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat">

        <!-- override the default theme for DialogFragments -->
        <item name="android:dialogTheme">@style/AppTheme.Dialog</item>

    </style>

    <!--
        configure your custom theme for DialogFragments...
        use a theme that has MinWidth, so that the dialog is not "too small"
    -->
    <style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">

        <!-- override the default theme for DialogFragments spawned by this DialogFragment -->
        <item name="android:dialogTheme">@style/AppTheme.Dialog</item>

        <!--
            OPTIONAL: override the background for the dialog...i am using a dark theme,
            and for some reason, there is no themes for dialogs with dark backgrounds,
            so, i made my own.
        -->
        <item name="android:windowBackground">@drawable/dialog__window_background</item>

        <!--
            add the title to the dialog's theme. you can remove it later by using
            DialogFragment.setStyle()
        -->
        <item name="android:windowNoTitle">false</item>
        <item name="windowNoTitle">?android:windowNoTitle</item>

    </style>

    .....

</resources>

如果您使用了暗色主题,并像我在AppTheme.Dialog中覆盖了android:windowBackground,那么请添加一个drawable/dialog__window_background.xml文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">
    <shape android:shape="rectangle">
        <corners android:radius="?dialogCornerRadius" />
        <solid android:color="?android:colorBackground" />
    </shape>
</inset>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接