我有一个约束视图作为我的Recycler视图中的一个视图。此约束视图包含2个textView和1个imageView。当我运行应用程序时,只显示图像视图而不显示文本。
我的xml -
<TextView
android:id="@+id/chatbot_text_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="11:40"
android:textSize="10sp"
app:layout_constraintEnd_toStartOf="@+id/chatbot_text_message_body"
tools:layout_editor_absoluteY="43dp" />
<TextView
android:id="@+id/chatbot_text_message_body"
android:layout_width="250dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/rounded_corner"
android:maxWidth="240dp"
android:padding="8dp"
android:textColor="#eff7de"
app:layout_constraintEnd_toStartOf="@+id/chatbot_imageView"
tools:layout_editor_absoluteY="3dp" />
<ImageView
android:id="@+id/chatbot_imageView"
android:layout_width="78dp"
android:layout_height="60dp"
tools:layout_editor_absoluteX="306dp"
tools:layout_editor_absoluteY="0dp" />
适配器中的代码 -
private void configureViewHolder2(ViewHolder2 viewHolder2, int position){
TextView tv1, tv2;
ImageView iv;
Log.i("RENCY POS", String.valueOf(position));
MainActivity.User2 user = (MainActivity.User2)mMessages.get(position);
String msg = user.getMessage();
Log.i("RENCY",msg);
DateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm");
Date date = new Date();
tv1 = viewHolder2.messageText;
tv2 = viewHolder2.messageTime;
iv = viewHolder2.chatbotImage;
tv1.setText(user.getMessage());
tv2.setText(dateFormat.format(date));
iv.setImageResource(R.drawable.chatbot);
}
public class ViewHolder2 extends RecyclerView.ViewHolder{
public ImageView chatbotImage;
public TextView messageText;
public TextView messageTime;
public ViewHolder2(View itemView){
super(itemView);
Log.i("RENCY","creating view holder");
chatbotImage = (ImageView) itemView.findViewById(R.id.chatbot_imageView);
messageText = (TextView) itemView.findViewById(R.id.chatbot_text_message_body);
messageTime = (TextView) itemView.findViewById(R.id.chatbot_text_message_time);
}
}
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position){
Log.i("RENCY", "VIEWTYPE "+String.valueOf(viewHolder.getItemViewType()));
switch(viewHolder.getItemViewType()){
case USER1:
ViewHolder1 vh1 = (ViewHolder1)viewHolder;
configureViewHolder1(vh1, position);
break;
case USER2:
ViewHolder2 vh2 = (ViewHolder2)viewHolder;
configureViewHolder2(vh2,position);
break;
default:
ViewHolder2 vh3 = (ViewHolder2)viewHolder;
configureViewHolder3(vh3,position);
break;
}
PS:用户1布局显示良好。它与User2类似,但它没有图像。只有2个文本
答案 0 :(得分:0)
实际上TextViews是ImageView的幕后推手!为您的ImageView设置此项:
app:layout_constraintTop_toBottomOf="@id/chatbot_text_message_body"
根本不使用layout_editor_absoluteY和layout_editor_absoluteX,因为它们没有编译到您的应用程序中(它们位于“工具”命名空间中) 你应该改变你的xml:
<TextView
android:id="@+id/chatbot_text_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="11:40"
android:textSize="10sp"
app:layout_constraintEnd_toStartOf="@+id/chatbot_text_message_body"
tools:layout_editor_absoluteY="43dp" />
<TextView
android:id="@+id/chatbot_text_message_body"
android:layout_width="250dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/rounded_corner"
android:maxWidth="240dp"
android:padding="8dp"
android:textColor="#eff7de"
app:layout_constraintEnd_toStartOf="@+id/chatbot_imageView"
tools:layout_editor_absoluteY="3dp" />
<ImageView
android:id="@+id/chatbot_imageView"
android:layout_width="78dp"
android:layout_height="60dp"
app:layout_constraintTop_toBottomOf="@id/chatbot_text_message_body"
tools:layout_editor_absoluteY="0dp" />
如果您只有两个TextView和一个ImageView,那么将您的constraintView放在ScrollView中,但如果你有很多TextViews和ImageViews,那么最好使用CardView。