自定义视图组中的视图未显示

2024-01-12

我最近深入研究创建自定义 ViewGroups 并遇到了一个我无法解决的问题。

我有 2 个 ViewGroups - ViewManager 和 Article

ViewManager 只是在上一篇文章下方布置一篇文章(即像垂直 LinearLayout)

正如您在图像中看到的那样,文章排列了几个 TextView 和一个 ImageView。创建一篇文章并将其添加到 ViewManager 工作正常,所有内容都会显示,但是当我添加第二篇文章时,该文章的所有内容都不可见。

那么为什么 TextView 或 ImageView 都没有显示出来(注意蓝色条是用 onDraw() 中的 canvas.drawRect() 绘制的)。我还打印了(通过logcat)子视图的绑定值(即drawChild()中的getLeft()/ Top()/ Right()/ Bottom(),它们看起来都很好

FIRST TextView
FIRST left 5 right 225 top 26 bottom 147
FIRST TextView
FIRST left 5 right 320 top 147 bottom 198
FIRST TextView
FIRST left 5 right 180 top 208 bottom 222
FIRST ImageView
FIRST left 225 right 315 top 34 bottom 129
SECOND TextView
SECOND left 10 right 53 top 238 bottom 257
SECOND TextView
SECOND left 5 right 325 top 257 bottom 349
SECOND TextView
SECOND left 5 right 320 top 349 bottom 400
SECOND TextView
SECOND left 5 right 180 top 410 bottom 424

那么有人知道我做错了什么吗?

测量方法文章

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec);

    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
    specWidth = widthSpecSize;
    int totalHeight=0;

    int width = 0;
    if(mImage!=null&&mImage.getParent()!=null){
        measureChild(mImage,MeasureSpec.makeMeasureSpec(100, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(100, MeasureSpec.AT_MOST));
        width=widthSpecSize-mImage.getWidth();
        imageWidth = mImage.getMeasuredWidth();
    }
    for(int i = 0;i<this.getChildCount();i++){
        final View child = this.getChildAt(i);

        //get the width of the available view minus the image width
        if(imageWidth > 0)
            width =widthSpecSize- imageWidth; 
        else
            width = widthSpecSize;

        //measure only the textviews
        if(!(child instanceof ImageView)){
            measureChild(child, MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), heightMeasureSpec);
            //calculate total height of the views, used to set the dimension
            totalHeight+=child.getMeasuredHeight();
        }
    }
    //if the title height is greater than the image then the snippet
    //can stretch to full width
    if(mTitle.getMeasuredHeight()>mImage.getMeasuredHeight())
        measureChild(mSnippet, MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.AT_MOST), heightMeasureSpec);

    //measure source to make it full width
    measureChild(mSource,MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.AT_MOST), heightMeasureSpec);
    setMeasuredDimension(widthSpecSize, totalHeight);
}

和 onLayout 方法

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

    int newLeft = left+outerMargin;
    int newTop = top+marginTop;
    int prevHeight = 0;

    if(mEngine!=null){

        int height = mEngine.getMeasuredHeight();
        int childRight = newLeft+mEngine.getMeasuredWidth(); 
        mEngine.layout(newLeft+marginLeft, newTop+prevHeight+2, childRight+marginLeft, newTop+height+prevHeight+2);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height+2;
        topBarHeight = mEngine.getMeasuredHeight()+(marginLeft*2);
        maxBottom = Math.max(maxBottom, mEngine.getBottom());
    }
    if(mTitle!=null){
        int height = mTitle.getMeasuredHeight();
        int childRight = newLeft+mTitle.getMeasuredWidth();
        mTitle.layout(newLeft, newTop+prevHeight, childRight, newTop+height+prevHeight);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mTitle.getBottom());
    }
    if(mSnippet!=null){
        int height = mSnippet.getMeasuredHeight();
        int childRight = newLeft+mSnippet.getMeasuredWidth();
        mSnippet.layout(newLeft, newTop+prevHeight, right, newTop+height+prevHeight);
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mSnippet.getBottom());
    }
    if(mSource !=null){
        int height = mSource.getMeasuredHeight();
        int childRight = newLeft+mSource.getMeasuredWidth();
        mSource.layout(newLeft, newTop+prevHeight+(marginTop*2), childRight, newTop+height+prevHeight+(marginTop*2));
        maxRight = Math.max(maxRight, childRight);
        prevHeight += height;
        maxBottom = Math.max(maxBottom, mSource.getBottom());

    }
    if(mImage!=null){
        int height = mImage.getMeasuredHeight();
        log("mxW "+maxRight);
        int childRight = maxRight+mImage.getMeasuredWidth();
        mImage.layout(right-mImage.getMeasuredWidth()+5, newTop+topBarHeight, right-5, height+topBarHeight);
        totalWidth = childRight;
        maxBottom = Math.max(maxBottom, mImage.getBottom());
    }else
        totalWidth = maxRight;

}

附带说明一下,可以像这样使用 LayoutParams.WRAP_CONTENT 作为 makeMeasureSpec() 的参数吗?

MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.AT_MOST)

In onLayout,孩子的位置应该相对于他们的父母。您正在添加top (and left)到孩子们的坐标。这对于第一篇文章来说不是问题,因为top and left为零。对于第二条,left仍然为零但是top是第二篇文章最上面的ViewManager中的。只要摆脱newTop and newLeft并分别使用marginTop and outerMargin在他们的地方。

关于你的旁白:第一个论点makeMeasureSpec应该是一个维度。通过使用 WRAP_CONTENT,您将最大尺寸设置为 -2,这可能不是您想要做的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义视图组中的视图未显示 的相关文章

  • 适用于 IOS 和 Android 的支付网关 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在开发一个应用程序 用户必须在澳大利亚餐馆通过应用程序 android ios 付款 有两种付款方式 通过 PayPal 或 Visa
  • Flutter 中的 AndroidManifest 中缺少默认通知通道元数据

    我在用firebase messaging 5 0 1软件包来实现推送通知 在 IOS 中一切正常 而在 Android 中 当我的移动应用程序在后台运行时 我收到通知 但它没有导航到相应的屏幕 它只是打开默认屏幕 如何实现到该特定屏幕的导
  • 使用 dpi 与 dp 缩放图像之间的差异

    我拥有所有由九个补丁位图组成的 dpi 可绘制目录 xxhdpi 和 xxxhdpi 是否必要 可绘制目录中的可绘制资源文件可检索所有缩放的位图 并且我使用可绘制资源文件 现在 我的问题是我还根据大小 小 正常等 创建了 缩放 布局目录 其
  • 让协程等待之前的调用

    我还没有完全掌握 Kotlin 协程 基本上我希望协程在执行之前等待任何先前的调用完成 下面的代码似乎可以工作 但它正在做我认为它正在做的事情吗 private var saveJob Job null fun save saveJob s
  • 使用 HttpClient 时,为什么服务器响应中省略了 Content-Length 标头?

    我正在使用这个问题的源代码如何异步执行httprequest并显示下载响应的进度 https stackoverflow com questions 9594318 how to asynchronous perform a httpreq
  • Android 中的 Sugar ORM:更新 SQLite 中保存的对象

    我是在 Android 上使用 SQLite 和 Sugar ORM 进行应用程序开发的新手 并尝试阅读 Sugar ORM 文档 但没有找到有关如何更新 SQLite 中保存的对象的任何信息 更改对象属性后还可以保存对象吗 就像是 Cus
  • 如何在活动中的必填字段中显示 * 符号

    我需要在活动中的必填字段中显示 符号 你能建议我怎样才能做到这一点吗 任何帮助 将不胜感激 我想说 作为必填字段的标记不遵循本机 Android 主题 的组合setHint and setError对于 Android 应用程序来说看起来更
  • 使用 HttpUrlConnection Android 将 base64 编码的图像发送到服务器

    我正在尝试使用 HttpUrlConnection 将 base64 编码的图像发送到服务器 我遇到的问题是大多数图像均已成功发送 但有些图像会生成 FileNotFound 异常 我的图像编码代码可以在下面找到 public static
  • 在 Android Lollipop 中从 Uri 中裁剪照片后总是返回 Null?

    我尝试在拍照或挑选照片后从 Uri 中裁剪图像 我的代码是这样的 public static void cropImage Uri uri Activity activity int action code Intent intent ne
  • 使用 Android Firebase 堆栈推送通知

    我开发了使用 Firebase 接收推送通知的 Android 应用程序 我的代码基于 Firebase Google 官方文档 https firebase google com docs cloud messaging android
  • Android Fragment onCreateView 与手势

    我正在尝试在片段中使用手势 我在 FragmentActivity 中有以下内容来处理我的详细信息片段 我试图发生的情况是 当在视图上检测到滑动时 将该视图内的数据替换为上一个或下一个条目 如果有更好的方法来处理这个问题 我完全同意 然而
  • Emma 不生成coverage.ec

    我设置了艾玛 它曾经对我有用 然后我们更改了源代码 现在它没有生成coverage ec根本不 它确实生成coverage em 测试临近结束时 出现错误消息 exec INSTRUMENTATION CODE 0 echo Downloa
  • android中listview显示数据库中的数据

    我是安卓新手 我想知道如何在列表视图中显示数据库中的数据 它不会向数据库添加数据 我只是显示我们存储在数据库中的任何内容 请帮助我实现这一目标 提前致谢 使用这些课程可能会对您有所帮助 用于数据库创建 package com example
  • Android 时钟滴答数 [赫兹]

    关于 proc pid stat 中应用程序的总 CPU 使用率 https stackoverflow com questions 16726779 total cpu usage of an application from proc
  • Android 中的列表视图分页

    我有一个列表视图 其中显示了 50 个元素 我决定对视图进行分页 以便视图的每个部分都有 10 个元素 然后单击 下一个 按钮以获取下一个 10 个元素 如何设置10个数据 我关注这篇文章http rakhi577 wordpress co
  • 未解决的包含:“cocos2d.h” - Cocos2dx

    当我在 Eclipse 中导入 cocos2dx android 项目时 我的头文件上收到此警告 Unresolved inclusion cocos2d h 为什么是这样 它实际上困扰着我 该项目可以正确编译并运行 但我希望这种情况消失
  • SDK >=26 仍需要 mipmap/ic_launcher.png?

    在 Android 中 有两种指定启动器图标 可以说是应用程序图标 的方法 老 方式 在 mipmap 文件夹中指定不同的 png 文件 通常命名为 ic launcher png 但可以通过以下方式设置名称android icon mip
  • Glass 语音命令给定列表中最接近的匹配项

    使用 Glass 您可以通过 确定 Glass 菜单启动应用程序 它似乎会选择最接近的匹配项 除非命令相距数英里 并且您可以明显看到命令列表 无论如何 是否可以从应用程序内或从语音提示 在初始应用程序触发后 给出类似的列表并返回最接近的匹配
  • Android Root 执行 su 带参数

    我在使用参数执行 su 时遇到问题 包含空格 我的 Command java 看起来像这样 public class Command Process process public String executeCommand String c
  • 如何在Android中创建一个简洁的两栏输入表单?

    我想创建一个整洁的两列输入表单 如下所示 到目前为止我的 xml 布局代码

随机推荐

  • T-SQL 脚本 - 时间线的逻辑问题

    创建并加载两个临时表 这是架构 Create table SH date datetime sched id int agent id int Create table SD sched id int start minute int le
  • Java表达式语言如何解析布尔属性? (在 JSF 1.2 中)

    所以我们都知道 someBean value 将尝试获取某些属性的内容someBean called value 它会寻找getValue 但是 如果这个属性是boolean 它会寻找isValue 它不会寻找的是hasValue 这让我思
  • BitmapImage 缺少 BeginInit() 和 EndInit() 函数?

    据我发现 我似乎无法访问上述功能 我正在导入 BitmapImage 类 using System Windows Media Imaging 但是在下面的代码中 public static void loadCardImage Card
  • 无法使用 Virtualenv 通过 pip 安装

    以下是我运行时遇到的错误pip serkan rm r mysite serkan pwd Users serkan Desktop Python Folder serkan virtualenv mysite New python exe
  • 传统 Web 应用程序和 API 中的身份验证、授权和会话管理

    如果我错了 请纠正我 在传统的 Web 应用程序中 浏览器会自动将会话信息附加到向服务器发出的请求中 以便服务器可以知道该请求来自谁 实际上到底附加了什么 但是 在基于 API 的应用程序中 此信息不会自动发送 因此在开发 API 时 我必
  • 运行 Android Studio gradle 构建时如何使用所有 CPU 核心/线程?

    我正在 Android Studio 中寻找参数或配置 Gradle 它可以设置构建 以便在构建期间使用我的所有 CPU 核心 即 如果我有一个四核 CPU 并且每个核心运行 8 个线程 我如何优化构建 以便它将使用它可以获得的所有资源 当
  • Matlab M 脚本可以通过脚本中的语句停止吗?

    一个非常简单且可能显而易见的问题 如何使用脚本中的语句中止 Matlab M 脚本的执行 这类似于调用return在函数中间立即结束它 If return http www mathworks com help matlab ref ret
  • 性能监控Openerp

    我们正在尝试实施新的遗物 http www newrelic com http www newrelic com 在开发服务器上测试 openerp 的性能 newrelic的以下安装步骤要求我们修改WSGI应用程序文件 我是 opener
  • TDD:单元测试异步调用

    guys 我正在开发一个应用程序 并通过单元测试来构建它 但是 我现在处于需要测试异步调用的情况 例如 void testUserInfoBecomesValidWhenUserIsBuiltSuccessfully if userBuil
  • go build 工作正常但 go run 失败

    我在主包的一个目录下有几个文件 主程序 配置文件 服务器 go 当我这样做时 去构建 程序构建完美并且运行良好 当我这样做时 go run main go 失败了 Output command line arguments main go
  • 如何让自托管 signalR 服务器作为 NetCore 控制台应用程序运行

    我想使用 NetCore 在控制台应用程序中创建 SignalR 自托管服务器 我对 Web 开发和 Net Core 完全陌生 但想使用 SignalR 作为基于 Web 的实时协议 不需要网页 所以我想要一个控制台应用程序 我已经成功测
  • 出售苹果股票(一进一出)

    任何人都可以帮我解决我的代码吗 我在卖苹果时遇到问题 它减去我的所有股票 有时当我的输入超过股票时 数字会变成负数 int main void int choice 0 int days 1 i buyApple int stocks 99
  • IntelliJ 中的远程结对编程

    结对编程时有没有办法使用 IntelliJ 如果有 v7 和 v8 的选项就好了 但如果需要的话我会升级到 v9 这个问题有点像this https stackoverflow com questions 926349 remote pai
  • gradle 测试执行时间过长

    我有一个 Spring boot 应用程序 可以在 10 秒内启动 然而 在一个简单的空集成测试中 我的执行时间很糟糕 60 70 秒 使用 IntelliJ 或时也是如此gradle test spring上下文的初始化比手动运行服务花费
  • Elixir + Ecto:不在 [array] 中怎么办?

    我正在尝试寻找所有Users 中没有特定的字符串元素match history场地 我对此进行了猜测 matched user User gt where u device id not in u match history gt limi
  • 如何从 Checkbox.checked 上的 Gridview 获取 Id?

    我有 GridView 和一个按钮 如下所示 然后我将 gridview 与数据库中的数据绑定 GridView 有两个隐藏字段 Id 和 ClassIndex 当我选择一个复选框并单击按钮时 我想获取相应的 ID 和文件名
  • 如何解决 JDK 的这个限制?

    我正在从 Java Collection Framework 中寻找一个不允许 null 元素的类 你认识一个吗 Use Constraints import com google common collect Constraints Co
  • 聊天机器人 - 使用 Facebook 登录为不属于您的页面生成页面令牌

    有谁知道如何在您不拥有的页面上安装 Facebook 聊天机器人 我已经创建了一个聊天机器人并希望允许其他FB页面安装我的聊天机器人应用程序 我可以为我管理的FB页面生成page access token 但是如何获取我不是管理员的FB页面
  • 如何在生产模式下运行 GWT

    我正在尝试按照以下说明在生产模式下运行 GWT 项目https developers google com web toolkit usingeclipse https developers google com web toolkit u
  • 自定义视图组中的视图未显示

    我最近深入研究创建自定义 ViewGroups 并遇到了一个我无法解决的问题 我有 2 个 ViewGroups ViewManager 和 Article ViewManager 只是在上一篇文章下方布置一篇文章 即像垂直 LinearL