[英]Align ImageView to center of a TextView in RelativeLayout
I have a Relative Layout as below: 我有一个相对布局如下:
<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>
The ImageView @+id/myImage
needs to align to the center of @+id/text2
TextView! ImageView
@+id/myImage
需要与@+id/text2
TextView的中心对齐! Currently, it is aligning to the top of @+id/text2
and is aligning to the layout's parentBottom
. 目前,它正在与
@+id/text2
的顶部对齐, 并且正在与布局的parentBottom
。 I don't want this hack. 我不想要这个黑客。
Is there a way of aligning the ImageView
to the TextView
's center ? 有没有办法将
ImageView
与TextView
的中心对齐?
This is a perfect case for a compound drawable (which is a best practice, to flatten your design). 这是复合拉丝的完美案例(这是最佳实践,可以展平您的设计)。
The ImageView goes INSIDE the TextView text2, at its bottom: ImageView位于TextView text2的底部:
Just add this to your text2 definition: 只需将其添加到text2定义:
android:drawableBottom="@drawable/logo"
You can add some padding, too 您也可以添加一些填充
android:drawablePadding="4dp"
The image is horizontally aligned to its container. 图像与其容器水平对齐。
If the text grows vertically, the image will be pulled down (as the TextView grows). 如果文本垂直增长,图像将被拉下(随着TextView的增长)。
To have the image left aligned and vertically centered, just change drawableTop with drawableLeft and you're done: 要使图像保持对齐并垂直居中,只需使用drawableLeft更改drawableTop即可:
android:drawableLeft="@drawable/logo"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.