Android 中缩小(或裁剪)对话框背景

2024-01-24

我有一个高分辨率图像(假设为 1900x1200),我想将其用作对话框的背景。它被创建了

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);

对话框布局:

<LinearLayout 
   android:id="@+id/dialog"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical" >

但是,如果我使用此图像,对话框(布局)将扩展以覆盖图像(或达到全屏,以较小者为准)。如果我将图像设置得非常小(例如 320x200),则放大效果很好。我需要的是,布局应该只包含内容,而不是背景图像。图像应该自动缩小或裁剪到对话框大小,没关系,它是纹理。我不想以编程方式设置/缩放(如果可以避免),因为我想在所有对话框中使用此背景,而且我懒于更改代码,而不是 XML :) 也绝对不想创建各种分辨率,因为对话框可以从非常小到全屏,所以不可能猜测大小。

我尝试过的:

添加到线性布局: android:background="@drawable/your_image这将扩展对话框以适应图像,这是我不想要的。

使用框架布局。作为第一个子元素,添加 ImageView,然后添加 LinearLayout。在 ImageView 中设置以下内容。

android:src="@drawable/your_image"
android:scaleType = "centerCrop" //I tried ALL scaleType

在这里尝试了所有技巧: 缩放背景图像以包裹布局内容 https://stackoverflow.com/questions/4761800/scale-background-image-to-wrap-content-of-layout不工作。

我相信对于这个非常简单且频繁的请求有一个非常简单的解决方案,我只是想不出来。

这是我的完整对话框布局源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:orientation="vertical"
android:minWidth="300dp" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back_dialog">

<EditText
    android:id="@+id/edittext_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<EditText
    android:id="@+id/edittext_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<CheckBox
    android:id="@+id/checkBox_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/IDS_ABC" />

<LinearLayout 
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button_ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/IDS_OK" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/IDS_CANCEL" />

</LinearLayout>
</LinearLayout>

EDIT这是为了更好地理解的图片


解决方案的想法来自这里:如何包装内容视图而不是背景可绘制? https://stackoverflow.com/questions/4312133/how-to-wrap-content-views-rather-than-background-drawable

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
   android:src="@drawable/back_dialog"
   android:scaleType="fitXY"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_alignTop="@+id/dialog_layout_main"
   android:layout_alignBottom="@id/dialog_layout_main"
   android:layout_alignLeft="@id/dialog_layout_main"
   android:layout_alignRight="@id/dialog_layout_main" />
<LinearLayout 
   android:id="@+id/dialog_layout_main"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
       .........
</LinearLayout>
</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中缩小(或裁剪)对话框背景 的相关文章

  • GCM 向主题发送消息:TOO_MANY_TOPICS 错误

    以前 GCM 每个应用程序有 100 万个主题订阅的限制 我发现他们现在已经取消了这一限制 基于发布 订阅模型 主题消息支持 每个应用程序无限订阅 https developers google com cloud messaging to
  • 导航组件重复 NavArgs 的问题

    我有一个片段 class SomeFragment private val args by navArgs
  • 如何从 SQLite 获取记录总数

    我正在尝试从 Sqlite DB 获取行的总数 以下是我想要做的代码片段 我不知道我在这里做错了什么 public static int getTotalCount Context context Cursor c null try c g
  • 共同的偏好不断消失

    我正在使用共享首选项来存储我的应用程序的登录凭据 除了一个用户之外 一切正常 一段时间后 共享偏好似乎会以某种方式重置或清除 我已针对该用户调整了我的应用程序 使其不再清除他的共享偏好设置 这样我就可以确定这不是我的应用程序的错 但即使在这
  • Android:“dp”到“px”转换?

    我正在读这篇文章 http developer android com guide practices screens support html http developer android com guide practices scre
  • 无法在自定义 AOSP 上安装 Google Play 中的某些应用程序:项目不可用。理由:9

    我在尝试从 Google Play 安装某些应用程序时收到以下错误 LibraryUtils isAvailable not available restriction 9 DocUtils getAvailabilityRestricti
  • 在 Google Analytics 中跟踪应用程序版本

    我正在使用谷歌分析模块 https marketplace appcelerator com apps 5081 2014113336 https marketplace appcelerator com apps 5081 2014113
  • 在意图过滤器中使用多个操作时的默认值

    尝试理解 Android 中的意图和操作并查看文档 http developer android com guide topics intents intents filters html 但我一直看到的一件事是定义了多个操作的意图过滤器
  • CookieManager.getInstance().removeAllCookie();不删除所有cookie

    我在应用程序的 onCreate 中调用 CookieManager getInstance removeAllCookie 我遇到了一个奇怪的问题 我看到 GET 请求中传递了意外的 cookie 值 事实上 cookie 值是一个非常非
  • 在 Jetpack Compose 中启动动画矢量 Drawable

    我有一个动画矢量可绘制R drawable my anim 我想在 Jetpack Compose 中展示并开始 可绘制对象显示 渲染正确 但动画未启动 这是撰写视图 Composable fun SplashView Surface mo
  • 带有自定义阵列适配器的微调器不允许选择项目

    我使用自定义阵列适配器作为微调器 但是 当在下拉列表中选择一个项目时 下拉列表保留在那里 并且微调器不会更新 这是错误行为 与使用带有字符串的通用数组适配器相比 这是自定义类 我错过了什么吗 谢谢 public class Calendar
  • Android 启动器快捷方式

    我制作了一个简单的打卡 打卡时钟应用程序 我想向用户添加在主屏幕上创建快捷方式的选项 该快捷方式将切换应用程序的状态 超时 超时 但我根本不希望此快捷方式在屏幕上打开应用程序 这是我的 setupShortcut private void
  • 检查 Android 手机上的方向

    如何查看Android手机是横屏还是竖屏 当前配置用于确定要检索的资源 可从资源中获取Configuration object getResources getConfiguration orientation 您可以通过查看其值来检查方向
  • 使用 Matrix.setPolyToPoly 选择位图上具有 4 个点的区域

    我正在 Android 上使用位图 在使用 4 个点选择位图上的区域时遇到问题 并非所有 4 点组都适合我 在某些情况下 结果只是一个空白位图 而不是裁剪后的位图 如图所示 并且 logcat 中没有任何错误 甚至是内存错误 这是我用来进行
  • 调节麦克风录音音量

    我们正在尝试调整录音时的音量级别 麦克风似乎非常敏感 会接收到很多静电 我们查看了 setVolumeControlStream 但找不到传入其中来控制麦克风的流 将您的音频源设置为 MIC using MediaRecorder Audi
  • Android 设备上的静默安装

    我已经接受了一段时间了 在 Android 上静默安装应用程序是不可能的 也就是说 让程序安装捆绑为 APK 的应用程序 而不提供标准操作系统安装提示并完成应用程序安装程序活动 但现在我已经拿到了 Appbrain 快速网络安装程序的副本
  • 下载后从谷歌照片库检索图像

    我正在发起从图库中获取照片的意图 当我在图库中使用 Nexus 谷歌照片应用程序时 一切正常 但如果图像不在手机上 在 Google Photos 在线服务上 它会为我下载 选择图像后 我将图像发送到另一个活动进行裁剪 但在下载的情况下 发
  • 将 JSON 参数从 java 发布到 sinatra 服务

    我有一个 Android 应用程序发布到我的 sinatra 服务 早些时候 我无法读取 sinatra 服务上的参数 但是 在我将内容类型设置为 x www form urlencoded 之后 我能够看到参数 但不完全是我想要的 我在
  • android Accessibility-service 突然停止触发事件

    我有一个 AccessibilityService 工作正常 但由于开发过程中的某些原因它停止工作 我似乎找不到这个原因 请看一下我的代码并告诉我为什么它不起作用 public class MyServicee extends Access
  • 如何删除因 Google Fitness API 7.5.0 添加的权限

    将我的 play services fitness api 从 7 0 0 更新到 7 5 0 后 我注意到当我将新版本上传到 PlayStore 时 它 告诉我正在添加一个新权限和 2 个新功能 我没有这样做 有没有搞错 在做了一些研究来

随机推荐