自定义对话框太小了

时间:2013-10-02 09:27:21

标签: 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_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;
}
}

12 个答案:

答案 0 :(得分:38)

在运行时更改对话框维度:

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

答案 1 :(得分:9)

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

答案 2 :(得分:5)

<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文件中使用此选项,并在对话框类

中使用此样式

答案 3 :(得分:5)

在onCreate

中加入以下行
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);

original answer

答案 4 :(得分:2)

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

答案 5 :(得分:1)

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

答案 6 :(得分: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();

答案 7 :(得分: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" >

答案 8 :(得分:0)

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

答案 9 :(得分:0)

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

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

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

答案 10 :(得分:0)

这个技巧对我有用

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

答案 11 :(得分:-2)

您可以在下面添加标签

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

在您的父级布局内部,因此dilog布局会尽可能填充屏幕宽度。

注意android:layout_width="600dp"的值应该大于屏幕显示的值, 我认为600dp对于MobileScreens就足够了。