How to set images on the same row as two TextViews?

筅森魡賤 提交于 2020-08-08 05:25:08

问题


I'm trying to create the following layout:

Basically, I have two TextViews and two icons. The icons are on the same row as the mobile header and the the mobile number. I have created the following XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/phone_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/primaryDarkColor"
        android:textStyle="bold"
        android:text="Phone number"
        android:textSize="18sp"
        android:layout_marginBottom="5dp" />

    <TextView
        android:id="@+id/phone_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_alignBottom="@id/phone_header" />

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/phone_info"
        android:layout_alignEnd="@id/phone_info"
        app:srcCompat="@drawable/ic_call_white_24dp" />

    <ImageView
        android:id="@+id/message_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/phone_icon"
        android:layout_alignEnd="@id/phone_icon"
        app:srcCompat="@drawable/ic_meessage_white_24dp" />

</RelativeLayout>

But it didn't set the layout properly. What is the best way would be to set the icons on the same row as the two TextViews?


回答1:


I made something like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mobile" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+1 32004 1001" />

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_baseline_info_24" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_baseline_info_24" />


</LinearLayout>

And it looks like this:

Or just do it like this:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_toStartOf="@+id/img1"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mobile" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+1 32004 1001" />

    </LinearLayout>

    <ImageView
        android:layout_toStartOf="@+id/img2"
        android:id="@+id/img1"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_baseline_info_24" />

    <ImageView
        android:adjustViewBounds="true"
        android:id="@+id/img2"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_baseline_info_24" />
</RelativeLayout>

Second way I think is better and ll not affect your icons if the screen is small and text wide




回答2:


You can simply do this using constraint layout like below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/color_black"
    android:padding="10dp"
    android:layout_height="wrap_content"
    tools:context=".ui.activities.MainActivity">
    <TextView
        android:id="@+id/phone_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:text="Mobile"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/phone_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        android:text="+1 302024 101"
        android:textColor="@color/color_white"
        android:layout_marginTop="2dp"
        app:layout_constraintTop_toBottomOf="@id/phone_header"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/message_icon"
        app:layout_constraintBottom_toBottomOf="@id/message_icon"
        android:layout_marginEnd="5dp"
        app:layout_constraintEnd_toStartOf="@id/message_icon"
        app:srcCompat="@drawable/ic_check_white_24dp" />

    <ImageView
        android:id="@+id/message_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_error_white_24dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

output:




回答3:


This gets you

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"

        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1 " />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2 " />
    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical|end"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Icon 1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Icon 2 " />

    </androidx.appcompat.widget.LinearLayoutCompat>


</androidx.appcompat.widget.LinearLayoutCompat>

design



来源:https://stackoverflow.com/questions/62353940/how-to-set-images-on-the-same-row-as-two-textviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!