<i id='WYaRs'><tr id='WYaRs'><dt id='WYaRs'><q id='WYaRs'><span id='WYaRs'><b id='WYaRs'><form id='WYaRs'><ins id='WYaRs'></ins><ul id='WYaRs'></ul><sub id='WYaRs'></sub></form><legend id='WYaRs'></legend><bdo id='WYaRs'><pre id='WYaRs'><center id='WYaRs'></center></pre></bdo></b><th id='WYaRs'></th></span></q></dt></tr></i><div id='WYaRs'><tfoot id='WYaRs'></tfoot><dl id='WYaRs'><fieldset id='WYaRs'></fieldset></dl></div>
      <bdo id='WYaRs'></bdo><ul id='WYaRs'></ul>
  • <legend id='WYaRs'><style id='WYaRs'><dir id='WYaRs'><q id='WYaRs'></q></dir></style></legend>

        <small id='WYaRs'></small><noframes id='WYaRs'>

      1. <tfoot id='WYaRs'></tfoot>

      2. 使用 ImageView 显示 AlertDialog 而无需任何填充

        时间:2023-08-30
          <bdo id='TkWLe'></bdo><ul id='TkWLe'></ul>
          • <i id='TkWLe'><tr id='TkWLe'><dt id='TkWLe'><q id='TkWLe'><span id='TkWLe'><b id='TkWLe'><form id='TkWLe'><ins id='TkWLe'></ins><ul id='TkWLe'></ul><sub id='TkWLe'></sub></form><legend id='TkWLe'></legend><bdo id='TkWLe'><pre id='TkWLe'><center id='TkWLe'></center></pre></bdo></b><th id='TkWLe'></th></span></q></dt></tr></i><div id='TkWLe'><tfoot id='TkWLe'></tfoot><dl id='TkWLe'><fieldset id='TkWLe'></fieldset></dl></div>

              <small id='TkWLe'></small><noframes id='TkWLe'>

                    <tbody id='TkWLe'></tbody>

                  <legend id='TkWLe'><style id='TkWLe'><dir id='TkWLe'><q id='TkWLe'></q></dir></style></legend>
                • <tfoot id='TkWLe'></tfoot>

                  本文介绍了使用 ImageView 显示 AlertDialog 而无需任何填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  编辑 - 解决方案:

                  我最终找到了解决此问题的方法.因为手动更改 ImageView 的高度会删除额外的填充,所以我最终找到了原始图像的尺寸,并在可以计算 ImageView 尺寸后将它们应用于 ImageView.这是最终结果:

                  I ended up figuring out a way to solve this issue. Because manually changing the height of the ImageView removes the extra padding, I ended up finding the dimensions of the original image, and apply them to the ImageView once the ImageView dimensions can be calculated. Here is the final result:

                  这是最终的工作代码:

                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          });
                          final AlertDialog dialog = builder.create();
                          LayoutInflater inflater = getLayoutInflater();
                          View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
                          dialog.setView(dialogLayout);
                          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  
                          dialog.show();
                  
                          dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                              @Override
                              public void onShow(DialogInterface d) {
                                  ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                                  Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.whygoprodialogimage);
                                  float imageWidthInPX = (float)image.getWidth();
                  
                                  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                                          Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                                  image.setLayoutParams(layoutParams);
                  
                  
                              }
                          });
                  

                  还有 XML:

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content">
                  
                      <ImageView
                          android:id="@+id/goProDialogImage"
                          android:layout_width="wrap_content"
                          android:layout_height="350dp"
                          android:src="@drawable/whygoprodialogimage"/>
                  
                  </LinearLayout>
                  

                  原始问题:

                  我的目标是有一个 AlertDialog ,它有一个 ImageView 占据整个对话框,保留它的尺寸,加上两个按钮.通过标准方法实现,如下所示:

                  My goal is to have an AlertDialog that has an ImageView that takes up the whole dialog, retaining it's dimensions, plus two buttons. By implementing it through standard methods, it looks like the following:

                  我正在尝试消除它上方和下方的填充.下面是它的设置代码:

                  I am trying to eliminate that padding above and below it. Here's the code for how it's set up:

                  布局:

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content">
                  
                      <ImageView
                          android:id="@+id/goProDialogImage"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:src="@drawable/whygoprodialogimage"/>
                  
                  </LinearLayout>
                  

                  代码:

                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          });
                          AlertDialog dialog = builder.create();
                          LayoutInflater inflater = getLayoutInflater();
                          View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
                          dialog.setView(dialogLayout);
                          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  
                          dialog.show();
                  

                  查看this StackOverflow answer后,我尝试实施该解决方案,因为他们的问题似乎相似,但即使它是更接近我现在想要的结果如下所示:

                  After looking at this StackOverflow answer, I tried implementing that solution because their problem seemed similar, but even though it is closer to what I want now the result looks like this:

                  所以它消除了填充,但图像看起来被压扁了.这是此潜在修复实现的代码:

                  So it eliminated the padding but the image looks squished. Here's the code for this potential fix implementation:

                  // Get screen size
                          Display display = getWindowManager().getDefaultDisplay();
                          Point size = new Point();
                          display.getSize(size);
                          int screenWidth = size.x;
                          int screenHeight = size.y;
                  
                          // Get target image size
                          Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.whygoprodialogimage);
                          int bitmapHeight = bitmap.getHeight();
                          int bitmapWidth = bitmap.getWidth();
                  
                          // Scale the image down to fit perfectly into the screen
                          // The value (250 in this case) must be adjusted for phone/tables displays
                          while(bitmapHeight > (screenHeight - 250) || bitmapWidth > (screenWidth - 250)) {
                              bitmapHeight = bitmapHeight / 2;
                              bitmapWidth = bitmapWidth / 2;
                          }
                  
                          // Create resized bitmap image
                          BitmapDrawable resizedDialogImage = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false));
                  
                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                  
                              }
                          }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          });
                          AlertDialog dialog = builder.create();
                          LayoutInflater inflater = getLayoutInflater();
                          View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
                          dialog.setView(dialogLayout);
                          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  
                          // Without this line there is a very small border around the image (1px)
                          dialog.getWindow().setBackgroundDrawable(null);
                  
                          dialog.show();
                          ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                          image.setBackground(resizedDialogImage);
                  

                  是什么导致图像现在看起来被压扁了?你可以看出它去掉了额外的填充,但图像尺寸发生了变化.

                  What is it that is causing the image to now look squished? You can tell it got rid of the extra padding but the image dimensions are changed.

                  推荐答案

                  我最终找到了解决这个问题的方法.因为手动更改 ImageView 的高度会删除额外的填充,所以我最终找到了原始图像的尺寸,并在可以计算 ImageView 尺寸后将它们应用于 ImageView.这是最终结果:

                  I ended up figuring out a way to solve this issue. Because manually changing the height of the ImageView removes the extra padding, I ended up finding the dimensions of the original image, and apply them to the ImageView once the ImageView dimensions can be calculated. Here is the final result:

                  这是最终的工作代码:

                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                          builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                              }
                          });
                          final AlertDialog dialog = builder.create();
                          LayoutInflater inflater = getLayoutInflater();
                          View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
                          dialog.setView(dialogLayout);
                          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  
                          dialog.show();
                  
                          dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                              @Override
                              public void onShow(DialogInterface d) {
                                  ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                                  Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.whygoprodialogimage);
                                  float imageWidthInPX = (float)image.getWidth();
                  
                                  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                                          Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                                  image.setLayoutParams(layoutParams);
                  
                  
                              }
                          });
                  

                  还有 XML:

                  <?xml version="1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content">
                  
                      <ImageView
                          android:id="@+id/goProDialogImage"
                          android:layout_width="wrap_content"
                          android:layout_height="350dp"
                          android:src="@drawable/whygoprodialogimage"/>
                  
                  </LinearLayout>
                  

                  这篇关于使用 ImageView 显示 AlertDialog 而无需任何填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Android Image Dialog/Popup 与图像大小相同且无边框 下一篇:android对话框活动位置

                  相关文章

                  最新文章

                  <legend id='bEWve'><style id='bEWve'><dir id='bEWve'><q id='bEWve'></q></dir></style></legend>
                    <i id='bEWve'><tr id='bEWve'><dt id='bEWve'><q id='bEWve'><span id='bEWve'><b id='bEWve'><form id='bEWve'><ins id='bEWve'></ins><ul id='bEWve'></ul><sub id='bEWve'></sub></form><legend id='bEWve'></legend><bdo id='bEWve'><pre id='bEWve'><center id='bEWve'></center></pre></bdo></b><th id='bEWve'></th></span></q></dt></tr></i><div id='bEWve'><tfoot id='bEWve'></tfoot><dl id='bEWve'><fieldset id='bEWve'></fieldset></dl></div>

                    <tfoot id='bEWve'></tfoot>

                    <small id='bEWve'></small><noframes id='bEWve'>

                      <bdo id='bEWve'></bdo><ul id='bEWve'></ul>