繁体   English   中英

如何在相对布局中将 Textview 放置在 Imageview 的右侧和中心

[英]How to place a Textview to the right and center of the Imageview in Relative Layout

我想将 textview 放置在图像视图的右侧和重心处。 我为此使用相对布局。 使用相对布局,我可以将 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>

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

<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>

您可以将其添加到您的 textview

android:layout_centerInParent="true"

使用此代码:

<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>

你想要这个视图吗? 然后试试这个代码。

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:layout_width="match_parent">

    <LinearLayout
        android:id="@+id/leaner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginStart="30dp"
            android:contentDescription="TODO"
            app:srcCompat="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/name_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name:"
                android:textColor="@android:color/black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/val_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Value"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/leaner1"
        android:layout_marginTop="1dp"
        android:background="@android:color/holo_blue_dark"
        android:text="button"
        android:textColor="@android:color/white" />

</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM