Android 如何在相对布局中将文本视图放置在Imageview的右侧和中心

Android 如何在相对布局中将文本视图放置在Imageview的右侧和中心,android,android-relativelayout,Android,Android Relativelayout,我想将textview放置在图像视图的右侧和重心处。我使用的是相对布局。使用相对布局,我可以将文本视图设置为图像视图的右侧,但无法将其设置为中间。我的意思是这里的中心不在Imageview的内部,我想将Textview放置在Imageview的右中心(Imageview的外部中心)。我无法设置为中心。 任何人请帮助我。我只想在相对布局中这样做 这是我的xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/

我想将textview放置在图像视图的右侧和重心处。我使用的是相对布局。使用相对布局,我可以将文本视图设置为图像视图的右侧,但无法将其设置为中间。我的意思是这里的中心不在Imageview的内部,我想将Textview放置在Imageview的右中心(Imageview的外部中心)。我无法设置为中心。 任何人请帮助我。我只想在相对布局中这样做

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/name_text_view"
        android:textSize="20sp"
        android:layout_toRightOf="@id/image_view"
        android:layout_width="wrap_content"
        android:textColor="@color/black"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
    <ImageView
        android:id="@+id/image_view"
        android:layout_marginLeft="30dp"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
    <Button
        android:id="@+id/button"
        android:layout_centerHorizontal="true"
        android:background="@color/blue"
        android:textColor="@color/color_white"
        android:layout_marginTop="20dp"
        android:layout_below="@id/name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/val_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button"/>
</RelativeLayout>

您可以将其添加到文本视图中

android:layout_centerInParent="true"

您可以使用以下代码执行此操作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">


<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="center"
    android:src="@drawable/ic_launcher_background" />


<TextView
    android:id="@+id/name_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/image_view"
    android:layout_centerInParent="true"
    android:text="hello world"
    android:textColor="@color/black"
    android:textSize="12sp" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:layout_width="match_parent">
  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      >
    <ImageView
        android:id="@+id/image_view"
        android:layout_marginLeft="30dp"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
  <TextView
      android:id="@+id/name_text_view"
      android:textSize="20sp"
      android:layout_toRightOf="@id/image_view"
      android:layout_width="wrap_content"
      android:textColor="@color/black"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"/>
  </LinearLayout>
  <Button
      android:id="@+id/button"
      android:layout_centerHorizontal="true"
      android:background="@color/blue"
      android:textColor="@color/color_white"
      android:layout_marginTop="20dp"
      android:layout_below="@id/name_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <TextView
      android:id="@+id/val_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@color/black"
      android:layout_marginTop="20dp"
      android:textSize="18sp"
      android:layout_centerHorizontal="true"
      android:layout_below="@id/button"/>
</RelativeLayout>

使用以下代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">


<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="center"
    android:src="@drawable/ic_launcher_background" />


<TextView
    android:id="@+id/name_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/image_view"
    android:layout_centerInParent="true"
    android:text="hello world"
    android:textColor="@color/black"
    android:textSize="12sp" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:layout_width="match_parent">
  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      >
    <ImageView
        android:id="@+id/image_view"
        android:layout_marginLeft="30dp"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
  <TextView
      android:id="@+id/name_text_view"
      android:textSize="20sp"
      android:layout_toRightOf="@id/image_view"
      android:layout_width="wrap_content"
      android:textColor="@color/black"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"/>
  </LinearLayout>
  <Button
      android:id="@+id/button"
      android:layout_centerHorizontal="true"
      android:background="@color/blue"
      android:textColor="@color/color_white"
      android:layout_marginTop="20dp"
      android:layout_below="@id/name_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <TextView
      android:id="@+id/val_text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@color/black"
      android:layout_marginTop="20dp"
      android:textSize="18sp"
      android:layout_centerHorizontal="true"
      android:layout_below="@id/button"/>
</RelativeLayout>

您想要此视图吗?然后试试这个代码