在我的应用程序中,我创建的自定义对话框没有完整的高度,我无法更改和自定义。例如,请看这个屏幕截图:
我的代码:
final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog);
contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_header_dialog_background"
android:orientation="horizontal"
android:padding="4dp" >
<TextView
android:id="@+id/TextView21"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:layout_weight="2"
android:gravity="center_vertical|right"
android:text="@string/choose_schedule_time_date"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ImageView03"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="0dp"
android:background="@drawable/icon_scudule_time" />
</LinearLayout>
</LinearLayout>
风格:
<style name="theme_sms_receive_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
</style>
答案 0 :(得分:65)
您需要获取当前窗口并将其设置为LayoutParams
,如下所示:
Dialog d=new Dialog(mContext);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
备选方案(如果上述解决方案无效)
如果上述代码效果不佳,您可以使用解决方法
styles.xml
<style name="mydialog"></style>
爪哇
Dialog d=new Dialog(mContext,R.style.mydialog);
.........
.........
myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
答案 1 :(得分:64)
这可以通过两种方式完成,第一种在style.xml中,第二种在代码中:
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog"> <item name="android:windowMinWidthMajor">90%</item> <item name="android:windowMinWidthMinor">90%</item> </style>
final Dialog contacts_dialog = new Dialog(ActivityGroup.this, R.style.theme_sms_receive_dialog); contacts_dialog.setContentView(R.layout.dialog_schedule_date_time);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
contacts_dialog.setCancelable(true);
contacts_dialog.setCanceledOnTouchOutside(true);
contacts_dialog.show();
答案 2 :(得分:7)
对于全角度对话框,您可以为对话框创建自定义样式。下面给出了全宽度对话框的代码:
<style name="Custom_Dialog" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
</style>
要将此样式指定给对话框的构造函数,请在onCreate()
后立即将其添加到setContentView()
方法:
getWindow()
.setLayout(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
答案 3 :(得分:4)
您不需要为此添加任何样式
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_layout_here);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();
注意:使用空的style
会浪费处理器内部的内存时间。而是直接使用dailog.getWindow().setLayout(width,height)
。
答案 4 :(得分:3)
如果有人想知道如何对使用DialogFragment
显示的对话框执行此操作,则可以覆盖onCreateDialog
以返回具有windowMinWidthMajor
并且次要设置为90的自定义样式的对话框%
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.WideDialog);
}
风格:
<style name="WideDialog" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
答案 5 :(得分:2)
在我的情况下,自定义对话框的宽度在大小(宽度)上缩小,因为即使设置了width属性,对话框内的内容的宽度也会变小
android:layout_width="match_parent"
我只是将宽度固定为屏幕尺寸,现在根据我的要求进行操作
android:layout_width="320dp"
答案 6 :(得分:2)
使用“相对布局”而不是“线性”布局来获得“自定义对话框”的完整宽度。
答案 7 :(得分:1)
要打开对话框时添加代码
final Dialog mBottomSheetDialog = new Dialog(getActivity(), R.style.MaterialDialogSheet);
mBottomSheetDialog.setContentView(R.layout.dialog_mainscreen_filter); // your custom view.
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
mBottomSheetDialog.show();
ImageView img_cross = mBottomSheetDialog.findViewById(R.id.img_cross);
final ImageView img_new = mBottomSheetDialog.findViewById(R.id.img_new);
final ImageView img_Used = mBottomSheetDialog.findViewById(R.id.img_Used);
img_cross.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBottomSheetDialog.dismiss();
}
});
img_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img_new.setImageResource(R.drawable.checkbox_tick);
img_Used.setImageResource(R.drawable.checkbox_tick_gray);
}
});
img_Used.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img_Used.setImageResource(R.drawable.checkbox_tick);
img_new.setImageResource(R.drawable.checkbox_tick_gray);
}
});
对话框的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="5dp"
app:cardElevation="@dimen/margin_10">
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/margin_10">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/filter"
android:textStyle="bold"
android:textColor="@color/colorGreen"
android:textSize="@dimen/font_large" />
<ImageView
android:id="@+id/img_cross"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/cross" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_20"
android:background="@color/colorfaintGreen" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_20"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_new"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/checkbox_tick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_10"
android:text="@string/New"
android:textColor="@color/colorText"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
<ImageView
android:id="@+id/img_Used"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="@dimen/margin_30"
android:src="@drawable/checkbox_tick_gray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_10"
android:text="@string/Used"
android:textColor="@color/colorText"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/margin_20"
android:background="@color/colorfaintGreen" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_20"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200+"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="@dimen/margin_20"
android:src="@drawable/filled_green" />
<SeekBar
android:id="@+id/seekbar"
style="@style/SeekBarWithoutSteps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:layout_marginTop="@dimen/margin_10"
android:max="4"
android:maxHeight="@dimen/margin_5"
android:minHeight="@dimen/margin_5"
android:paddingLeft="@dimen/margin_10"
android:paddingRight="@dimen/margin_10"
android:progressBackgroundTint="@color/colorGray"
android:progressTint="@color/colorGreen"
android:theme="@style/Widget.AppCompat.SeekBar.Discrete"
android:thumb="@drawable/filled_green"
android:thumbOffset="15dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/search_near_me_in_km"
android:textColor="@color/colorTextGray"
android:textSize="@dimen/fontsize_normal"
android:textStyle="bold" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="@dimen/btn_height"
android:layout_marginBottom="@dimen/margin_10"
android:layout_marginTop="@dimen/margin_10"
android:background="@drawable/btn_bg_green_rounded"
android:text="@string/submit"
android:textColor="@color/colorWhite"
android:textSize="@dimen/fontsize_medium" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Style.xml ------------------------------------------
<style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
</style>
<style name="MaterialDialogSheetAnimation">
<item name="android:windowEnterAnimation">@anim/popup_show</item>
<item name="android:windowExitAnimation">@anim/popup_hide</item>
</style>
在res文件夹中添加动画: anim / popup_show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%p"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0" />
</set>
anim / popup_hide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toYDelta="100%p" />
</set>
答案 8 :(得分:1)
Dialog dialog = new Dialog(BASE_CONTEXT, R.style.Theme_Dialog);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.your_layout);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
答案 9 :(得分:0)
@Override public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow()
.setLayout((int) (getScreenWidth(getActivity()) * .9), (int)(getScreenHeight(getActivity()) * .6) );
}
}
public static int getScreenWidth(Activity activity) {
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
return size.x;
}
public static int getScreenHeight(Activity activity) {
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
return size.y;
}
90%宽度和60%高度的例子
答案 10 :(得分:0)
MyDialogFragment myDialogFragment =
new MyDialogFragment(activity,arraylist.get(position).getImage());
myDialogFragment.show();
Window window = myDialogFragment.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
答案 11 :(得分:0)
这可以视为设置布局宽度(例如1000dp)和高度换行内容的简单技巧 就像:
const module: TestingModule = await Test.createTestingModule({
providers: [{
provide: OperationDocumentService,
useValue: {
findById: () => OperationDocumentMock,
},
}]
});
答案 12 :(得分:0)
与其使用自定义对话框,不如使用活动类。在清单文件中,如下指定样式属性
android:theme="@style/AppThemeDialog"
并按如下所示添加AppThemeDialog
<style name="AppThemeDialog" parent="Theme.AppCompat">
<item name="colorBackgroundFloating">#ff424242</item>
<item name="listPreferredItemPaddingLeft">0dp</item>
<item name="android:windowIsFloating">true</item>
<item name="listPreferredItemPaddingRight">0dp</item>
</style>
答案 13 :(得分:0)
答案 14 :(得分:0)
只需尝试将对话框布局包装在RelativeLayout中
答案 15 :(得分:-1)
您可以通过编程方式设置对话框的宽度和高度。
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_create_subcategory);
dialog.getWindow().setLayout(width, height);