【问题标题】:Align ImageView to center of a TextView in RelativeLayout在 RelativeLayout 中将 ImageView 对齐到 TextView 的中心
【发布时间】:2014-03-24 02:43:36
【问题描述】:

我有一个如下的相对布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="0.38"
    android:background="@drawable/layout"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:layout_marginLeft="80dp"
        android:textColor="@color/text1color"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text1"
        android:layout_below="@+id/text1"
        android:background="@drawable/text2_background"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:textColor="@color/text2color"
        android:textSize="13sp" />

    <com.widgets.CustomImageView
        android:id="@+id/myImage"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="0dp"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/text2"
        android:src="@drawable/logo" />

</RelativeLayout>

ImageView @+id/myImage 需要对齐到@+id/text2 TextView 的中心!目前,它与@+id/text2 的顶部对齐并且 与布局的parentBottom 对齐。我不想要这个黑客。

有没有办法将ImageViewTextView中心对齐?

【问题讨论】:

  • 尝试了您的建议。无法解决。有什么解决办法吗?

标签: android android-imageview android-relativelayout


【解决方案1】:

这是复合可绘制对象的完美案例(这是一种最佳实践,可以使您的设计扁平化)。
ImageView 进入 TextView text2 的底部:

只需将其添加到您的 text2 定义中:

android:drawableBottom="@drawable/logo"

你也可以添加一些填充

android:drawablePadding="4dp"

图像与其容器水平对齐。
如果文本垂直增长,图像将被下拉(随着 TextView 的增长)。

要使图像左对齐并垂直居中,只需将 drawableTop 更改为 drawableLeft 即可:

android:drawableLeft="@drawable/logo"

【讨论】:

  • 谢谢。我给这个问题增加了一些复杂性——它是一个自定义 ImageView!现在,如何将其添加到 drawableLeft
  • 我做到了。没用。布局看起来很糟糕。我将android:layout_alignParentBottom="true" 添加到customImageView。它有帮助。但是中心对齐没有发生。 :(
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 2018-12-30
相关资源
最近更新 更多