【问题标题】:How do I re-position and ImageView on a layout with a TextInputLayout and EditText?如何使用 TextInputLayout 和 EditText 在布局上重新定位和 ImageView?
【发布时间】:2015-10-20 07:20:44
【问题描述】:

我有一个垃圾桶图标,我在 ImageView 中用作可绘制对象。垃圾桶出现在我不想要的布局线的顶部(图1)。添加 'layout_gravity="bottom"' 将图标一直向下移动到布局的底部(图 2)。我希望将图标放置在与“到期日期”文本的水平底部完全匹配的位置,并位于到期日期 EditText 行(Pic3)末尾的右侧。我尝试添加边距和填充但没有运气。有什么想法吗?

部分xml文件:

...
<LinearLayout
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:contentDescription="Trash can"
    android:src="@drawable/ic_delete_black_24dp"  />
...

Pic1

Pic2

图三

图四

【问题讨论】:

  • 尝试 (android:layout_marginRight="xdp" & android:layout_marginTop="xdp") 否则您可能需要一个围绕 ImageView 的 FrameLayout,然后您应该能够将 Bin 放置在您想要的任何位置。
  • 好的,但是如何让“截止日期”EditText 占据 90% 的宽度,而垃圾桶占据剩下的 10%?
  • 你不能,据我所知,android 不使用百分比。但是,您可以使用表格布局并将列拆分为不同的大小,以便编辑文本占用更多水平空间
  • 好的,谢谢,我会试试的。
  • 您也可以在 Photoshop 中设置图像的边距

标签: android android-layout android-edittext


【解决方案1】:

试试看:

trashIcon.setY(dateEdittext.getY-dateEdittext.getHeight/2);

【讨论】:

  • 嗨,有没有办法在 xml 布局文件中做到这一点?
【解决方案2】:

您可以使用布局权重来确保垃圾箱只能使用它需要的内容 (0),并将其余部分交给编辑文本 (1)

<LinearLayout
  <EditText
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
  />
  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:scaleType="centerInside"
    android:layout_weight="0"
    android:contentDescription="Trash can"
    android:src="@drawable/ic_delete_black_24dp"  />
</LinearLayout>

【讨论】:

  • 好的,但是在 EditText 的末尾和图标之间有相当多的空白(参见上面的图 4)。所以在 EditText 的末尾和/或 ImageView 的开头和结尾必须有一些默认的边距或填充。在这种情况下,将边距和填充显式设置为零有帮助吗?还有其他想法吗?
  • 另外,使用带有可绘制对象的 TextView 与您推荐的带有 src=drawable...的 ImageView 相比有什么缺点吗?
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多