gpt4 book ai didi

android - 尝试设置imageView的宽度以匹配TextView的宽度

转载 作者:行者123 更新时间:2023-12-02 13:44:54 25 4
gpt4 key购买 nike

我正在尝试聊天,并且我希望每个消息都具有与文本大小匹配的背景图像。

这是我的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>

我的聊天kt适配器:
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;
}
}

如您所见,我将bg宽度设置为文本的measuredWidth,但由于尚未测量 View ,因此无法使用。
我不知道如何解决它,因为我没有确定的消息数量,因此创建 View 时我无法做

最佳答案

如果要使用imageView作为背景,这是错误的,则可以在background中使用relativeLayout,如下所示:

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

关于android - 尝试设置imageView的宽度以匹配TextView的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com