Android 尝试将imageView的宽度设置为与TextView的宽度匹配
我正在尝试聊天,我希望每条消息都有一个与文本大小匹配的背景图像 这是我的xml:Android 尝试将imageView的宽度设置为与TextView的宽度匹配,android,android-studio,kotlin,Android,Android Studio,Kotlin,我正在尝试聊天,我希望每条消息都有一个与文本大小匹配的背景图像 这是我的xml: <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/msgBg" android:scaleType="fitXY" android:adju
<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;
}
}
class ChatAdapter(变量列表:ArrayList):RecyclerView.Adapter(){
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val image=itemView.Profile
val text=itemView.textMSG
var bg=itemView.msgBg
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val view=LayoutInflater.from(parent.context).充气(com.example.gamecompanyaleixazuela.R.layout.item_chat,parent,false)
返回视图保持器(视图)
}
重写getItemCount():Int{
返回list.count();
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
holder.text.text=列表[位置].text
holder.bg.layoutParams.width=holder.text.measuredWidth;
holder.bg.layoutParams.height=holder.text.measuredhight;
}
}
如您所见,我将bg宽度设置为文本的measuredWidth,但它不起作用,因为视图尚未测量。
我不知道如何修复它,因为我没有确定数量的消息,所以我无法在创建视图时进行更改。如果您想使用
imageView
作为背景,这是错误的,您可以在relativeLayout
中使用background
,如下所示:
<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>
如果您想使用
图像视图
作为背景,这是错误的,您可以在相对视图中使用背景
,如下所示:
<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>