我正在向 WindowManager 添加一个 View 并覆盖它的 onConfigurationChanged 函数,如下所示:
查看代码
// onConfigurationChanged should be called after config change has finished
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// also tried executing following in the `GlobalLayoutListener` without any difference
onConfigOrSystemUIChanged(newConfig)
}
fun onConfigOrSystemUIChanged(newConfig: Configuration?) {
val screen = getScreenSize(this)
// screen should hold current screen size
// but this sometimes contains the wrong size, the one from before the config change
L.d("[%s] Config changed (%s)", logBaseInfo, screen)
}
服务代码
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val screen = getScreenSize(this)
L.d("Config changed in SERVICE: Screen: %s | newConfig: %s", screen, newConfig);
}
问题
我可以看到这在大约 99% 的时间内都有效。我旋转我的设备,将其打开、关闭等等,一切正常。但是我时不时地看到它失败一次,这意味着我的 View 在设备旋转后读取了错误的屏幕尺寸。有什么想法吗?
附加信息 - 日志
我向服务的 onConfigurationChanged 和 View 的 onConfigurationChanged 添加了一些日志。我将 5 个 View 添加到 WindowManager 并查看以下内容:
[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(2076, 1080) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w692dp h336dp 480dpi nrml long hdr land finger -keyb/v/h -nav/h appBounds=Rect(144, 0 - 2220, 1080) s.839 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(2076, 1080))
=> [BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
// ... here some time that wents by ...
[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(1080, 2076) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w360dp h668dp 480dpi nrml long hdr port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2076) s.840 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(1080, 2076))
在这里您看到一个 View (在日志行的开头,第 6 行用 => 标记)确实得到了错误的屏幕尺寸,我没有不知道为什么...
附加信息 - 功能
fun getScreenSize(context: Context) {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val outSize = Point()
wm.getDefaultDisplay().getSize(outSize)
return outSize
}
最佳答案
这可能是因为在屏幕旋转尚未完成时测量 View 。尝试设置超时。
clearTimeout(t)
t = setTimeout(function(){/*do your stuff here*/},200);
这里的 t 是一个全局变量——如果屏幕在测量屏幕尺寸之前旋转,则为 clearTimeout。尝试使用以毫秒为单位的时间(在我的代码中为 200)来优化所需的行为。
关于android - WindowManager 中 View 的 onConfigurationChanged 工作不可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52926334/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall