对话框宽高问题
Dialog width and height issue
我使用自定义对话框,但在移动设备和选项卡视图中,但当我在选项卡上查看时设计发生了变化
我想在所有设备上使用相同的对话框。
看我下面的代码。
private void startDialog() {
final Dialog dialog = new Dialog(activity);
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.checkout_alert_dialog);
dialog.show();
dialog.getWindow().setLayout(width, height);
btn_cancle = dialog.findViewById(R.id.btn_cancle);
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, "Cancel coming soon", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
btn_continue = dialog.findViewById(R.id.btn_continue);
btn_continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, " Ok coming soon", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
}
下面是Dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_pointBox"
android:layout_width="@dimen/_80sdp"
android:layout_height="@dimen/_80sdp"
android:src="@drawable/ic_chekout_popup"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/_30sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/normal_input_text_size"
android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:layout_below="@+id/iv_pointBox"
android:layout_marginTop="@dimen/_15sdp"
/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/view_color"
android:layout_above="@+id/linerbutton"
/>
<LinearLayout
android:id="@+id/linerbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/btn_cancle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_cancel"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:gravity="center"
android:layout_weight="1"
/>
<View
android:layout_width="@dimen/_1sdp"
android:layout_height="@dimen/_45sdp"
android:background="@color/view_color"
/>
<Button
android:id="@+id/btn_continue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_continue"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
我提到了我的设备的 2 个屏幕截图:
移动设备屏幕截图:
标签截图:
屏幕截图对话框中的内容已更改。 那么我该如何解决
提前致谢:
尝试自定义布局
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>
<Button
android:id="@+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/textDialog"
android:layout_toRightOf="@+id/imageDialog"
/>
</RelativeLayout>
在 java 文件中
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");
打开对话框使用
dialog.show();
关闭对话框使用
dialog.dismiss();
替换
myAlertDialog.show();
来自
alertDialog.show();
然后
alertDialog.getWindow().setLayout(width,height);
它将完美运行
完整代码:
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("title");
myAlertDialog.setMessage("use this");
AlertDialog alertDialog = myAlertDialog.create();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) ((int)displaymetrics.widthPixels * 0.9);
int height = (int) ((int)displaymetrics.heightPixels * 0.7);
myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent pictureActionIntent = null;
pictureActionIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 15);
}
});
alertDialog.show();
alertDialog.getWindow().setLayout(width,height);
}
如果还是不行那么你可以这样使用:
class DialogView extends DialogFragment {
Context context;
public static DialogView newInstance() {
DialogView f = new DialogView();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);
context = this.getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()) {
@Override
public void onBackPressed() {
//do your stuff
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button_layout_view, container, false);
setCancelable(false);
// RelativeLayout closeBtn = (RelativeLayout) view.findViewById(R.id.bottom_separator); get button ids like this- gallery camera etc and you can use click listener on that
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = (int) (metrics.widthPixels * 0.90);
int height = (int) (metrics.heightPixels * 0.95);
this.getDialog().getWindow().setLayout(width, height);
return view;
}
public static DisplayMetrics getDeviceMetrics(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getMetrics(metrics);
return metrics;
}
}
为带有两个按钮和标题的 xml 创建
button_layout_view
然后这样调用:
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DialogFragment newFragment = DialogView.newInstance();
newFragment.show(ft, "");
就个人而言,我更喜欢创建 DialogFragment 并像这样设置宽度和高度:
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
int width = getArguments().getInt(BUNDLE_KEY_WIDTH);
int height = getArguments().getInt(BUNDLE_KEY_HEIGHT);
window.setLayout(width, height);
}
您的对话框的宽度和高度在两种设备上都很好(您在 startDialog()
方法中设置它们)。
如果您希望 ImageView 在平板设备上更小,以便移动设备和平板设备的比例相同,您应该为它们使用不同的 dimens
文件:
首先,为您的图像定义一个维度:
里面dimens.xml
<dimen name="dialog_image_size">XXXdp</dimen>
然后,在 /res/values-sw720dp/dimens.xml 内和 /res/values-sw600dp/dimens 内为 10'' 平板定义相同的尺寸.xml 7'' 平板电脑,并为其设置较小的 dp。
现在,当您使用平板电脑时,会加载不同的尺寸,因此您可以使用较小的尺寸来保持与移动设备相同的比例。
看看你的 layout.xml
你的布局 height
和 width
是 match_parent
然后它创建了一个空间。
只需将你的 xml
替换为我的 xml
。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/_250sdp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_pointBox"
android:layout_width="@dimen/_80sdp"
android:layout_height="@dimen/_80sdp"
android:src="@drawable/ic_chekout_popup"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/_30sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/normal_input_text_size"
android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:layout_below="@+id/iv_pointBox"
android:layout_marginTop="@dimen/_15sdp"
/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/view_color"
android:layout_marginTop="@dimen/_50sdp"
/>
<LinearLayout
android:id="@+id/linerbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_cancle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_cancel"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:gravity="center"
android:layout_weight="1"
/>
<View
android:layout_width="@dimen/_1sdp"
android:layout_height="@dimen/_45sdp"
android:background="@color/view_color"
/>
<Button
android:id="@+id/btn_continue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_continue"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
并删除 java
文件中的以下三行 无需在 运行 时间 更改 height
和 width
:
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
dialog.getWindow().setLayout(width, height);
我使用自定义对话框,但在移动设备和选项卡视图中,但当我在选项卡上查看时设计发生了变化
我想在所有设备上使用相同的对话框。
看我下面的代码。
private void startDialog() {
final Dialog dialog = new Dialog(activity);
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.checkout_alert_dialog);
dialog.show();
dialog.getWindow().setLayout(width, height);
btn_cancle = dialog.findViewById(R.id.btn_cancle);
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, "Cancel coming soon", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
btn_continue = dialog.findViewById(R.id.btn_continue);
btn_continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, " Ok coming soon", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
}
下面是Dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_pointBox"
android:layout_width="@dimen/_80sdp"
android:layout_height="@dimen/_80sdp"
android:src="@drawable/ic_chekout_popup"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/_30sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/normal_input_text_size"
android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:layout_below="@+id/iv_pointBox"
android:layout_marginTop="@dimen/_15sdp"
/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/view_color"
android:layout_above="@+id/linerbutton"
/>
<LinearLayout
android:id="@+id/linerbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/btn_cancle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_cancel"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:gravity="center"
android:layout_weight="1"
/>
<View
android:layout_width="@dimen/_1sdp"
android:layout_height="@dimen/_45sdp"
android:background="@color/view_color"
/>
<Button
android:id="@+id/btn_continue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_continue"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
我提到了我的设备的 2 个屏幕截图:
移动设备屏幕截图:
标签截图:
屏幕截图对话框中的内容已更改。 那么我该如何解决
提前致谢:
尝试自定义布局
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>
<Button
android:id="@+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/textDialog"
android:layout_toRightOf="@+id/imageDialog"
/>
</RelativeLayout>
在 java 文件中
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");
打开对话框使用
dialog.show();
关闭对话框使用
dialog.dismiss();
替换
myAlertDialog.show();
来自
alertDialog.show();
然后
alertDialog.getWindow().setLayout(width,height);
它将完美运行
完整代码:
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("title");
myAlertDialog.setMessage("use this");
AlertDialog alertDialog = myAlertDialog.create();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = (int) ((int)displaymetrics.widthPixels * 0.9);
int height = (int) ((int)displaymetrics.heightPixels * 0.7);
myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent pictureActionIntent = null;
pictureActionIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 15);
}
});
alertDialog.show();
alertDialog.getWindow().setLayout(width,height);
}
如果还是不行那么你可以这样使用:
class DialogView extends DialogFragment {
Context context;
public static DialogView newInstance() {
DialogView f = new DialogView();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);
context = this.getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()) {
@Override
public void onBackPressed() {
//do your stuff
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button_layout_view, container, false);
setCancelable(false);
// RelativeLayout closeBtn = (RelativeLayout) view.findViewById(R.id.bottom_separator); get button ids like this- gallery camera etc and you can use click listener on that
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = (int) (metrics.widthPixels * 0.90);
int height = (int) (metrics.heightPixels * 0.95);
this.getDialog().getWindow().setLayout(width, height);
return view;
}
public static DisplayMetrics getDeviceMetrics(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getMetrics(metrics);
return metrics;
}
}
为带有两个按钮和标题的 xml 创建
button_layout_view
然后这样调用:
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DialogFragment newFragment = DialogView.newInstance();
newFragment.show(ft, "");
就个人而言,我更喜欢创建 DialogFragment 并像这样设置宽度和高度:
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
int width = getArguments().getInt(BUNDLE_KEY_WIDTH);
int height = getArguments().getInt(BUNDLE_KEY_HEIGHT);
window.setLayout(width, height);
}
您的对话框的宽度和高度在两种设备上都很好(您在 startDialog()
方法中设置它们)。
如果您希望 ImageView 在平板设备上更小,以便移动设备和平板设备的比例相同,您应该为它们使用不同的 dimens
文件:
首先,为您的图像定义一个维度:
里面dimens.xml
<dimen name="dialog_image_size">XXXdp</dimen>
然后,在 /res/values-sw720dp/dimens.xml 内和 /res/values-sw600dp/dimens 内为 10'' 平板定义相同的尺寸.xml 7'' 平板电脑,并为其设置较小的 dp。
现在,当您使用平板电脑时,会加载不同的尺寸,因此您可以使用较小的尺寸来保持与移动设备相同的比例。
看看你的 layout.xml
你的布局 height
和 width
是 match_parent
然后它创建了一个空间。
只需将你的 xml
替换为我的 xml
。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/_250sdp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_pointBox"
android:layout_width="@dimen/_80sdp"
android:layout_height="@dimen/_80sdp"
android:src="@drawable/ic_chekout_popup"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/_30sdp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/normal_input_text_size"
android:text="You've chosen to redeem 865 Points and pay 44.44 AED. The Payment process might take Some times, please do not hit the back button or close the app. Would you like to continue. "
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:layout_below="@+id/iv_pointBox"
android:layout_marginTop="@dimen/_15sdp"
/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/_1sdp"
android:background="@color/view_color"
android:layout_marginTop="@dimen/_50sdp"
/>
<LinearLayout
android:id="@+id/linerbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_cancle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_cancel"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:gravity="center"
android:layout_weight="1"
/>
<View
android:layout_width="@dimen/_1sdp"
android:layout_height="@dimen/_45sdp"
android:background="@color/view_color"
/>
<Button
android:id="@+id/btn_continue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/lable_continue"
android:textColor="@color/black"
android:textSize="@dimen/normal_input_text_size"
android:textStyle="bold"
android:background="@null"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
并删除 java
文件中的以下三行 无需在 运行 时间 更改 height
和 width
:
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.70);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.65);
dialog.getWindow().setLayout(width, height);