文本块和图像的定位
我有三个文本块和一个图像,我希望将其放入草图中所示的布局中。 (暂时忽略红线。)
(我知道质量很糟糕。如果有人能为我推荐一个像样的(开放的)图形编辑器,用于 os x/linux,我将不胜感激!(不,不是 gimp!))
我的想法是解决这样的问题:
- 相对布局。
- 相对于第一个文本块定位第二个文本块。
例如:
android:layout_below=text1
android:layout_ (make it "float" to screen's right edge - not sure how to do that, yet.
- 相对于第一个和第二个块的第三个文本块。
例如:
android:layout_below=text2
android:layout_alignLeft = text1
好的。不,让我们来看图片。它的大小是任意的,所以我认为通过一些对齐我可以将其放入。如果你知道,请查看草图中的红线,我会考虑相应地对齐图像。
android:layout_alignTop = text2
android:layout_alignBottom = text2
android:layout_alignLeft = text1
然而,我不太擅长布局,当我尝试像这样实现它时,4 块内容或多或少地在屏幕上乱七八糟,但我从来没有把它们全部放在正确的位置。
我能实现的最接近的是这个,但我不喜欢在那里使用硬编码限制。恐怕它不是超级灵活。
尽管我必须在某种程度上限制图像的显示尺寸,但我希望通过周围的文本块,机器人能够足够聪明地自行计算出尺寸。
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="text1" />
<ImageView
android:id="image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="text1"
android:layout_marginRight="7dip"
android:layout_marginTop="5dip"
android:adjustViewBounds="true"
android:maxHeight="150sp"
android:maxWidth="150sp"
android:src="srcFile" />
<TextView
android:layout_marginTop="5dip"
android:id="text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="image"
android:layout_below="text1"
android:layout_toRightOf="image"
android:text="text2" />
<TextView
android:id="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="image"
android:layout_marginTop="10dip"
android:text="text3" />
因此,我想听听您对我的方法的看法,是否有一些我可以改进的地方以及如何实际实施它。
编辑: 所以我想找到一个可以删除这部分代码的解决方案,因为我担心它不够灵活:
android:maxHeight="150sp"
android:maxWidth="150sp"
I have three text blocks and an image which I would like to to fit in a layout as indicated in the sketch. (Ignore the red lines for a second.)
(I know the quality is awful. If anyone can recommend me a decent (open) graphic editor for os x/linux I'd appreciate it! (nope, not gimp!))
My idea was to tackle the problem like this:
- Relative Layout.
- position the second text block relative to the first one.
e.g.:
android:layout_below=text1
android:layout_ (make it "float" to screen's right edge - not sure how to do that, yet.
- The third text block relative to the 1st and 2nd block.
e.g.:
android:layout_below=text2
android:layout_alignLeft = text1
Ok. No let's get to the image. It's arbitrary size so I thought with some aligns I could fit it in. If you know look at the red lines from the sketch I though about aligning the image accordingly.
android:layout_alignTop = text2
android:layout_alignBottom = text2
android:layout_alignLeft = text1
However I'm not very good in doing layouts and when I tried to implement it like this the 4 pieces of content more or less knocked around on the screen but I never got them all into position.
The closest I could achieve was this but I don't like the usage of hard coded limits in there. I'm afraid it is not super flexible then.
though I have to limit the display size of the image somewhat I was hoping with the surrounded text blocks the droids would be smart enough to figure out the size by their own.
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="text1" />
<ImageView
android:id="image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="text1"
android:layout_marginRight="7dip"
android:layout_marginTop="5dip"
android:adjustViewBounds="true"
android:maxHeight="150sp"
android:maxWidth="150sp"
android:src="srcFile" />
<TextView
android:layout_marginTop="5dip"
android:id="text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="image"
android:layout_below="text1"
android:layout_toRightOf="image"
android:text="text2" />
<TextView
android:id="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="image"
android:layout_marginTop="10dip"
android:text="text3" />
So I'd like to hear what you think about my approach and if there's something I could improve as well as how to actually implement it.
edit:
So I'd like to find a solution where I could drop this part of the code because I'm afraid it's not flexible enough:
android:maxHeight="150sp"
android:maxWidth="150sp"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无需代码即可保持图像的比例并调整其大小。只需使用线性布局并相应地加权视图
It is possible to keep the proportions of the image and adjust its size without code. Simply use linear layouts and weight the views accordingly
简单的解决方案可能是最好的。使用线性布局,方向设置为垂直或水平:
Simple solutions are probably the best. Use linear layout with orientation set to vertical or horizontal:
就足够了
将图像 id 修复为
使用
并在第二个文本中
对于第三个仅
LinearLayouts...我也喜欢它们,它们很简单,但速度很慢 - 不要太多使用它们。
It would be enough to
repair image id as
and use
in the second text
For the third only
LinearLayouts... I like them too, they are easy, but slow - don't use them much.