Android 布局动态调整大小
对于我为程序设计的页面上的许多“标题”TextView,我希望它们成为父级。Width / 2,然后正确对齐。虽然在 Java 中编写代码相当容易,但我尝试在 XML 布局中尽可能避免 XML-Java 代码交叉,直到最后一点(按下按钮、完成页面等)。
我是否必须浏览每个页面并自己计算每个项目的具体宽度,或者有没有办法按照“fill_parent / 2”的方式放置一些内容?
编辑:忘记提及可能的关键说明 - 几乎我所做的一切都是在relativelayout中,我在这个项目中很少使用LinearLayouts。
For many "header" TextViews across the pages I'm designing for a program, I would like them to be the parent.Width / 2 then properly aligned. While that would be fairly easy to code in the Java, I am attempting to do as much as possible in the XML layouts to avoid XML-Java code intersections until the last little bits (button presses, finish page, etc).
Do I have to go through each page and calculate every item's specific width myself or is there a way to put something along the lines of "fill_parent / 2"?
EDIT: Forgot to mention what is likely a key note - almost everything I am doing is in RelativeLayouts, I have very little use for LinearLayouts in this project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有一个左右齐平的 LinearLayout,则可以执行以下操作:
通过在父级上设置weightSum,您可以说子级的权重应等于该值。如果将单个孩子的体重设置为一半,那么它就会占用一半的空间。确保将子项的宽度设置为 0,以便它知道使用权重值来计算其相对于父项的空间。
然后,您可以通过在父 LinearLayout 上设置重力来按照您想要的方式对齐它。
If you have a LinearLayout that is flush to the left and right, you can do the following:
By setting the weightSum on the parent, you're saying that the weights of the children should equal that amount. By setting the single child's weight to half of that, it'll take up half the space. Make sure to set the width of the child to 0 so it knows to use the weight value to calculate its space relative to its parent.
Then, you can align it however you'd like by setting gravity on the parent LinearLayout.
使用具有两列的表格视图,其中每列都被拉伸并具有文本视图。然后隐藏第二个textview
Use a tableview with two columns where each column is stretched and has a text view. Then hide the second textview