简体   繁体   中英

Trying to set the width of an imageView to match the width of a TextView

I'm trying to make a chat, and I want each message to have a background image that matches the text sizes.

This is my xml:

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

    <ImageView
        android:id="@+id/msgBg"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_textsms_black_24dp"
        >

    </ImageView>

    <TextView
        android:id="@+id/textMSG"
        android:layout_alignParentStart="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginLeft="@dimen/margin_big"
        android:text="example"
        android:layout_weight="70"
        android:textSize="@dimen/text_size_bigmedium"
        >

    </TextView>

</RelativeLayout>

My kt Adapter for the Chat:

class ChatAdapter(var list :ArrayList<ChatMessage>):RecyclerView.Adapter<ChatAdapter.ViewHolder>(){

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val image = itemView.Profile
        val text = itemView.textMSG
        var bg = itemView.msgBg
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(com.example.gamecompanionAleixAzuela.R.layout.item_chat, parent, false)

        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.count();
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.text.text = list[position].text
        holder.bg.layoutParams.width = holder.text.measuredWidth;
        holder.bg.layoutParams.height = holder.text.measuredHeight;
    }
}

As you can see i set the bg width to the measuredWidth of the text but it doesn't work as the view hasn't been measured yet. i don't know how to fix it as I don't have a determinate number of messages so i can't do a for when the view has been created

If you want to use imageView as background , this is wrong , you can use background in your relativeLayout like this :

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_textsms_black_24dp"
>

    <TextView
        android:id="@+id/textMSG"
        android:layout_alignParentStart="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginLeft="@dimen/margin_big"
        android:text="example"
        android:layout_weight="70"
        android:textSize="@dimen/text_size_bigmedium"
        >

    </TextView>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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