![](/img/trans.png)
[英]Is there a way to align TextView vertically center with ImageView in RelativeLayout
[英]Align ImageView to center of a TextView in RelativeLayout
我有一个相对布局如下:
<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
。 我不想要这个黑客。
有没有办法将ImageView
与TextView
的中心对齐?
这是复合拉丝的完美案例(这是最佳实践,可以展平您的设计)。
ImageView位于TextView text2的底部:
只需将其添加到text2定义:
android:drawableBottom="@drawable/logo"
您也可以添加一些填充
android:drawablePadding="4dp"
图像与其容器水平对齐。
如果文本垂直增长,图像将被拉下(随着TextView的增长)。
要使图像保持对齐并垂直居中,只需使用drawableLeft更改drawableTop即可:
android:drawableLeft="@drawable/logo"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.