如何在 androidX 中创建消息时显示图像..?

How to show image in the place of creating message in androidX..?

我正在创建一个离线短信 app.In 我必须放置选择图像按钮的选项并且在选择图像后我想将它放在发送的位置 image.But 不明白如何调整 imageView 按钮以获得所需 layout.Someone 可以帮助我..?

我搜索了很多,但一无所获。

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
            android:layout_width="match_parent"
            android:padding="5dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:orientation="horizontal" tools:ignore="MissingConstraints" android:id="@+id/linearLayout">

        <ImageView
                android:id="@+id/inbox_thumb"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/circle"/>
        <TextView
                android:hint="Phone Number"
                android:layout_width="5dp"
                android:layout_marginTop="6dp"
                android:padding="10dp"
                android:textStyle="bold"
                android:textSize="15dp"
                android:gravity="left"
                android:layout_height="wrap_content" android:id="@+id/phone" android:layout_weight="1"/>


    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView android:layout_width="384dp"
                                               android:layout_height="661dp"
                                               android:scrollbarAlwaysDrawVerticalTrack="true"
                                               android:id="@+id/reyclerview_message_list"
                                               android:layout_marginLeft="16dp"
                                               android:layout_marginStart="16dp"
                                               app:layout_constraintStart_toStartOf="parent"
                                               app:layout_constraintLeft_toLeftOf="parent"
                                               app:layout_constraintRight_toRightOf="parent"
                                               app:layout_constraintTop_toBottomOf="@+id/linearLayout">

    </androidx.recyclerview.widget.RecyclerView>

    <View android:layout_width="0dp"
          android:layout_height="2dp"
          tools:ignore="MissingConstraints"
          android:background="@color/colorPrimaryDark"
          android:layout_marginBottom="0dp"
          app:layout_constraintBottom_toTopOf="@+id/message_area"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent">
    </View>

    <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content"
                  android:id="@+id/message_area"
                  android:orientation="horizontal"
                  android:minHeight="48dp"
                  android:background="#ffffff"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintRight_toRightOf="parent"
                  app:layout_constraintLeft_toLeftOf="parent"
                  tools:ignore="MissingConstraints">
        <Button
                android:text="attach img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/btn1"
        />
        <EditText
                android:id="@+id/txtMessage"
                android:hint="Enter message"
                android:background="@android:color/transparent"
                android:layout_gravity="center"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:maxLines="6"/>
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/imageView2"
                android:layout_weight="1"/>
        <Button
                android:id="@+id/btnSend"
                android:text="SEND"
                android:textSize="14dp"
                android:clickable="true"
                android:layout_width="64dp"
                android:layout_height="48dp"
                android:gravity="center"
                android:layout_gravity="bottom" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

预期

实际

如果我没理解错的话,您是想将 "imageView2" ImageView 放在 "txtMessage" EditText 之上,对吗?

如果是这样,您需要做的是将它们都包装在一个垂直的 LinearLayout 中,如下所示:

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <EditText
            android:id="@+id/txtMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:hint="Enter message"
            android:maxLines="6" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@mipmap/ic_launcher" />
    </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:orientation="horizontal"
    android:padding="8dp">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_drawable"
        android:backgroundTint="@android:color/black"
        android:padding="8dp"
        android:layout_margin="8dp"
        android:src="@drawable/ic_add_black_24dp"
        android:tint="@android:color/white" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/darker_gray"
        app:cardCornerRadius="6dp"
        app:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:src="@mipmap/ic_launcher" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="bottom"
                android:weightSum="1">

                <EditText
                    android:id="@+id/et1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toStartOf="@+id/ib2"
                    android:layout_margin="8dp"
                    android:text="Hello" />

                <ImageButton
                    android:id="@+id/ib2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/circle_drawable"
                    android:backgroundTint="@android:color/black"
                    android:padding="8dp"
                    android:layout_margin="8dp"
                    android:layout_alignParentEnd="true"
                    android:src="@drawable/ic_send_black_24dp"
                    android:tint="@android:color/white" />

            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

我已经使用背景drawable,你可以替换为你的添加和发送图像。 希望能解决你的问题。

我已经使用您在 AndroidX 中的布局创建了一个基本模板。您可以相应地使用您的样式、图标和颜色来美化它。请复制并在您的 Android 工作室中查看。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:orientation="horizontal"
    android:padding="5dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/inbox_thumb"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/phone"
        android:layout_width="5dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:layout_weight="1"
        android:gravity="left"
        android:hint="Phone Number"
        android:padding="10dp"
        android:textSize="15dp"
        android:textStyle="bold" />


</LinearLayout>


<LinearLayout
    android:id="@+id/message_area"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@drawable/rounded_rect_shape"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">


    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:src="@drawable/ic_launcher" />

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:cardBackgroundColor="@android:color/darker_gray"
        app:cardCornerRadius="6dp"
        app:cardElevation="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/ic_launcher" />

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/txtMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@android:color/transparent"
            android:hint="Enter message"
            android:maxLines="6" />

    </LinearLayout>

    </androidx.cardview.widget.CardView>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="center"
        android:text="SEND"
        android:textSize="14dp" />
</LinearLayout>

<View
    android:layout_width="0dp"
    android:layout_height="2dp"
    android:layout_marginBottom="0dp"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintBottom_toTopOf="@+id/message_area"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/reyclerview_message_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@id/message_area"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"/>