Android 如何使TextView相对于ImageView在中心垂直浮动?
我试图使包含文本“This is some long text”(这是一些长文本)的文本视图相对于左侧的图像浮动在中间,但我的当前外观如下: 此TextView最多可以占用2行文本,因此如果它有2行,我希望将其恢复为标准对齐方式,如: 这是我现在的布局:Android 如何使TextView相对于ImageView在中心垂直浮动?,android,android-layout,Android,Android Layout,我试图使包含文本“This is some long text”(这是一些长文本)的文本视图相对于左侧的图像浮动在中间,但我的当前外观如下: 此TextView最多可以占用2行文本,因此如果它有2行,我希望将其恢复为标准对齐方式,如: 这是我现在的布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/user_left"
android:layout_width="20dp"
android:layout_height="20dp"
android:scaleType="centerCrop"
android:src="@drawable/the_pirates"
android:layout_margin="5dp"
/>
<TextView android:id="@+id/relationship_caption"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="8dp"
android:text="This is some long text"
android:layout_toRightOf="@id/user_left"
android:layout_margin="5dp"/>
</RelativeLayout>
我试过将重力
设置为中心
,但似乎没有效果\
谢谢 用这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/user_left"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="@drawable/the_pirates" />
<TextView
android:id="@+id/relationship_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/user_left"
android:layout_toRightOf="@+id/user_left"
android:maxLines="2"
android:text="This is some long text"
android:textSize="15dp" />
</RelativeLayout>
在这里,我已经做了你想做的事
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp" >//here you can give your size
<ImageView
android:id="@+id/user_left"
android:layout_width="120dp"//here you can put your size as you want
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/relationship_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/user_left"
android:maxLines="2"
android:text="This is some long text"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
//这里您可以给我您的尺码
@ASP将重力设置为垂直中心
无法实现此功能,因为我正在尝试将文本相对于左侧图像浮动到中心。setcenterVertical=“true”
在文本视图中,尝试将图像视图和文本视图放在水平线性布局中,然后再进行处理。@ASP Piyush设置alignTop的解决方案。但似乎也需要对齐底部。@ASP那个解决方案对我不起作用。我得到的是一个角落里的图像,但是下一个在屏幕的中间(而不是图像的中间)。Ya,你可以看到我也添加了一个。但是看起来“中心”纯粹是由边缘定义的。有没有办法让它垂直居中,即图像的顶部和底部?意思是在图像之间精确居中?明白了吗。我也已将底部对齐,并将重力设置为中心
。希望这是做事情的正确方式。我认为它需要对齐底部和对齐顶部。