Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义对话框太小_Android_Android Dialogfragment_Customdialog - Fatal编程技术网

Android 自定义对话框太小

Android 自定义对话框太小,android,android-dialogfragment,customdialog,Android,Android Dialogfragment,Customdialog,我有一个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_he

我有一个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" />

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

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

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);
试试这个

根据设备屏幕大小设置对话框宽度和高度

    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();

真的
真的
真的
@空的
@android:style/Animation.Dialog
状态未指定|调整盘
@android:彩色/透明
600dp

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

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

尝试将
android:layout\u height
设置为base
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" >

正确的解决方案是覆盖
onCreateDialog
方法,而不是
onCreateView
,并使用
AlertDialog.Builder
创建对话框,如文档中所述:

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

在onCreate中置于行下方

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
您可以在下面添加标签

    <Space
            android:layout_width="600dp"
            android:layout_height="1dp"/>

在父布局内部,因此dilog布局尽可能地填充屏幕宽度

请注意,android:layout_width=“600dp”
value应该大于屏幕宽度,
我认为600dp对于MobileScreen来说已经足够了。

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

将应用程序的主题设置为
AndroidManifest.xml
中的自定义主题……或者,将托管
对话框片段的
活动的主题设置为应用程序的自定义主题


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


@样式/AppTheme.Dialog
@样式/AppTheme.Dialog
@可绘制/对话框\窗口\背景
假的
?android:windowNoTitle
.....
如果您使用暗主题,并像我在
AppTheme.Dialog
中所做的那样覆盖了
android:windowBackground
,然后添加一个
drawable/Dialog\u window\u background.xml
文件,其内容如下:



发布您的java文件。您必须在java文件中设置对话框的宽度和高度。@我已经发布了。@mungaihkamau您解决问题了吗?@JaAd no,我仍在试图从中找出一些东西google@mungaihkamau请重试我的代码。您愿意帮助我根据您的解决方案编辑我的代码吗?您是如何初始化宽度的?@Chris代码只是“告诉”对话框以屏幕宽度的6/7和尖叫高度的4/5作为宽度。但是如果我给match parent作为宽度,还是问题?为什么?@ParthAnjaria抱歉,我没有注意到你的评论。我不知道,但可能不再允许使用此参数进行对话。我认为,即使这是有用的,拥有一个大对话框也不是一个好的解决方案(设计)。即使我的解决方案有效,也可能不再完美。如果你应该展示很多信息,那么重新思考,使用一个片段或一个活动。就是这样。非常感谢。使用的是
getActivity().getWindow()
而不是
getDialog().getWindow()
,请注意,当用户界面准备好时,必须在
onStart()
中调用它,而不是在
onCreate()
中。这里唯一对我有效的是minWidth=600dp。在我的例子中,我不需要其他样式元素。如果DialogFragment不使用
AlertDialog.Builder
,它将不会有任何帮助。是的,它可以工作,但1)对话框扩展到整个宽度,2)此主题已被弃用,您可以使用
主题\u DeviceDefault\u dialog\u MinWidth
主题\设备默认\灯光\对话框\操作栏\最小宽度
或任何材料。另外,在布局中设置
style
android:theme
也无法解决问题,因此我们应该使用此方法
setStyle()
。这是最好的解释!在自定义布局中,您不能使用“匹配父对象”,因为它始终是对话框本身使用的包装内容
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
    <Space
            android:layout_width="600dp"
            android:layout_height="1dp"/>