Как разместить Textview справа и в центре ImageView в относительной компоновке - PullRequest
0 голосов
/ 03 октября 2019

Я хочу разместить текстовое представление справа и в центре тяжести изображения. Для этого я использую Relative Layout. Используя Относительную компоновку, я могу установить 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>

Ответы [ 4 ]

1 голос
/ 03 октября 2019

Вы можете сделать это с помощью этого кода:

<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>
0 голосов
/ 03 октября 2019

Хотите посмотреть? затем попробуйте этот код.

enter image description here

<?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>
0 голосов
/ 03 октября 2019

Используйте этот код:

<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>
0 голосов
/ 03 октября 2019

Вы можете добавить это в текстовое представление

android:layout_centerInParent="true"
...