Custom dialog too small

后端 未结 11 2275
轻奢々
轻奢々 2020-12-10 10:53

I have an android activity that implements a custom dialog.The application is running ok but the dialog is too small,i want to display a bigger dialog.How can i achieve this

相关标签:
11条回答
  • 2020-12-10 11:39

    I configured my project so that the default dialog theme for DialogFragments has a minimum width.

    set the app's theme to your custom theme in the AndroidManifest.xml....alternatively, set the theme of the Activity hosting the DialogFragment to your app's custom theme.

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

    define your custom theme in values/styles.xml, and override the default theme used for dialog fragments with one you define yourself.

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

    if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:

    <?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>
    
    0 讨论(0)
  • 2020-12-10 11:42

    try to set android:layout_height to some value for base LinearLayout

    for example

    <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 讨论(0)
  • 2020-12-10 11:43

    Dialogs are sized by their content. You can add padding and margins on the outer layout to consume more space, but that will not redistribute the views inside.

    0 讨论(0)
  • 2020-12-10 11:44

    Add android:minWidth / android:minHeight to your root view. See example below:

    <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">
    
    0 讨论(0)
  • 2020-12-10 11:52

    Try this

    For setting dialog width and height as per device screen size

        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();
    
    0 讨论(0)
提交回复
热议问题