21

我有一个实现自定义对话框的 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" />

这是我的输出。我想增加对话框的宽度和高度。 在此处输入图像描述

还有我的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;
}
}
4

11 回答 11

57

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

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);
于 2013-10-02T09:34:29.257 回答
16

在自定义布局中设置 android:minWidth 和 android:minHeight 对我有用。

于 2015-02-25T08:39:27.717 回答
8

把下面的行放在 onCreate

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

原始答案

于 2017-06-05T10:15:52.540 回答
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文件中使用这个并在对话框类中使用这个样式

于 2014-03-14T05:49:18.950 回答
5

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

于 2013-10-02T09:32:16.220 回答
2

正确的解决方案是覆盖该onCreateDialog方法而不是onCreateView,并使用 a 创建对话框AlertDialog.Builder,如文档中所述:http: //developer.android.com/reference/android/app/DialogFragment.html#AlertDialog

于 2016-04-18T07:00:51.557 回答
2

添加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">
于 2017-05-18T01:20:59.210 回答
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)
    }
于 2021-07-24T19:34:44.913 回答
0

尝试这个

用于根据设备屏幕尺寸设置对话框宽度和高度

    Display display;
    int DisplayWidth, DisplayHeight, DialogWidth, DialogHeight;
    Dialog dialog;

display =((WindowManager)activity_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayWidth = display.getWidth();
        DisplayHeight = display.getHeight();

        if(DisplayHeight > DisplayWidth)
        {
            DialogWidth = (6 * DisplayWidth) / 7 ;
            DialogHeight = (4 * DisplayHeight) / 5 ;
        }
        else
        {
            DialogWidth = (6 * DisplayWidth) / 9 ;
            DialogHeight = (4 * DisplayHeight) / 5 ;
        }

        dialog = new Dialog(activity_context);

          // Set your dialog width and height dynamically as per your screen.

        Window window = dialog.getWindow();
        window.setLayout(DialogWidth,DialogHeight);
        window.setGravity(Gravity.CENTER);

        dialog.show();
于 2013-10-02T09:36:34.820 回答
0

尝试android:layout_height为基数设置一些值LinearLayout

例如

<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" >
于 2016-01-17T22:22:28.753 回答
0

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

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

<?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>

如果您使用深色主题,并android:windowBackground像我在 中所做的那样覆盖AppTheme.Dialog,则添加一个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>
于 2020-11-21T12:23:49.643 回答