【问题标题】:positioning of textblocks and images文本块和图像的定位
【发布时间】:2012-02-03 10:54:14
【问题描述】:

我有三个文本块和一个图像,我想将它们放入草图中所示的布局中。 (忽略红线一秒钟。)
(我知道质量很糟糕。如果有人能推荐我一个体面的(开放的)用于 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"

【问题讨论】:

    标签: android android-layout android-relativelayout android-imageview textview


    【解决方案1】:

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

    <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" />
    

    【讨论】:

      【解决方案2】:

      简单的解决方案可能是最好的。使用 linear layout 并将方向设置为垂直或水平:

      <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>
      

      【讨论】:

      • 那么您可以在运行时使用 Display 对象来获取屏幕尺寸
      【解决方案3】:

      足够了

      修复图片id为

      android:id="@+id/image"
      

      并使用

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

      在第二个文本中

      只给第三个

      android:layout_below="@+id/text2"
      

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

      【讨论】:

      • 我想我会编辑我的问题。我想要一个不依赖于使用硬编码值调整图像大小的解决方案。该解决方案仅在我手动设置图像大小限制时才有效。 :C
      • 不能无需代码即可同时保持图像的比例并调整其大小。
      • 问题已回答,错误已发现 - 请标记答案。因此,彻底改变的答案将是另一个答案。
      猜你喜欢
      • 2016-12-08
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2019-03-24
      • 1970-01-01
      • 2018-04-09
      相关资源
      最近更新 更多