文本块和图像的定位

发布于 2025-01-01 15:32:59 字数 2208 浏览 2 评论 0原文

我有三个文本块和一个图像,我希望将其放入草图中所示的布局中。 (暂时忽略红线。) 在此处输入图像描述
(我知道质量很糟糕。如果有人能为我推荐一个像样的(开放的)图形编辑器,用于 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.)
enter image description here
(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2025-01-08 15:32:59

无需代码即可保持图像的比例并调整其大小。只需使用线性布局并相应地加权视图

<TextView
       android:id="@+id/text1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="text1" />

   <LinearLayout
       android:id="@+id/ll"
       android:layout_below="@+id/text1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:weightSum="2"
       android:orientation="horizontal" >

       <ImageView
           android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="11"
           android:adjustViewBounds="true"
           android:src="srcFile" />

       <TextView
           android:id="@+id/text2"
           android:layout_weight="1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="text2" />
   </LinearLayout>

   <TextView
       android:id="@+id/text3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/ll"
       android:text="text3" />

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

<TextView
       android:id="@+id/text1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="text1" />

   <LinearLayout
       android:id="@+id/ll"
       android:layout_below="@+id/text1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:weightSum="2"
       android:orientation="horizontal" >

       <ImageView
           android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="11"
           android:adjustViewBounds="true"
           android:src="srcFile" />

       <TextView
           android:id="@+id/text2"
           android:layout_weight="1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="text2" />
   </LinearLayout>

   <TextView
       android:id="@+id/text3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/ll"
       android:text="text3" />
月寒剑心 2025-01-08 15:32:59

简单的解决方案可能是最好的。使用线性布局,方向设置为垂直或水平:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="text1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            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_weight="1"
            android:text="text2" />

    </LinearLayout>
    <TextView
        android:id="text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="text3" />
</LinearLayout>

Simple solutions are probably the best. Use linear layout with orientation set to vertical or horizontal:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="text1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            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_weight="1"
            android:text="text2" />

    </LinearLayout>
    <TextView
        android:id="text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="text3" />
</LinearLayout>
站稳脚跟 2025-01-08 15:32:59

就足够了

将图像 id 修复为

android:id="@+id/image"

使用

android:layout_toRightOf="@+id/image"
android:layout_below="@+id/text1"

并在第二个文本中

对于第三个仅

android:layout_below="@+id/text2"

LinearLayouts...我也喜欢它们,它们很简单,但速度很慢 - 不要太多使用它们。

It would be enough to

repair image id as

android:id="@+id/image"

and use

android:layout_toRightOf="@+id/image"
android:layout_below="@+id/text1"

in the second text

For the third only

android:layout_below="@+id/text2"

LinearLayouts... I like them too, they are easy, but slow - don't use them much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文