设置表面视图的大小和位置
我正在开发一个界面,其中屏幕的一半有编辑文本框供用户输入数据,另一半有一个可以绘制并读取用户触摸的画布。
现在,我正在扩展一个表面视图,并在运行时使用 View.Addview 将其添加到我的布局(布局是 XML)中。
我的问题是设置视图的大小和位置。我希望它是方形的并占据屏幕的大约一半(横向)。我希望它与右侧对齐并且垂直居中或与顶部对齐。
我可以通过重写 onMeasure 方法来设置大小,但我不知道如何获取显示大小,所以我不知道将其设置为什么。
我对如何对齐视图没有任何想法,我一直在尝试将其添加到各种布局中,但运气不佳......
感谢您的帮助!
I'm working on an interface where one half of the screen has edit text boxes for the user to enter data into, and the other half has a canvas that can be drawn to and reads user touches.
Right now I'm extending a surface view and adding that to my layout(Layout is XML) at runtime with View.Addview.
My problem is setting the size and position of the view. I want it to be square and take up about half of the screen (landscape). I want it to be aligned to the right side and either centered vertically, or aligned with the top.
I can set the size by overriding the onMeasure method, but I don't know how to get the display size so I don't know what to set it to.
I don't have any ideas about how to align the view, I've been trying adding it to various layouts without much luck...
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有同样的问题。你找到答案了吗?我找到了。只需修改 onlayout(),设置类似:
并且不要触摸 onmeasure()。
have same question. did you find answer?I figured it out. Just modify the onlayout(), set something like:
and do not touch onmeasure().
通常您不必显式布局组件。这是布局经理的职责。
您只需设置布局约束。
以下是获取屏幕尺寸的方法。
yourActivity.getWindowManager().getDefaultDisplay().getWidth();
Normally you do not have to explicitely layout components. Its the duty of layout manager.
You just have to set layout constraints.
Here is how to get the screen size.
yourActivity.getWindowManager().getDefaultDisplay().getWidth();