约束视图显示不佳

Constraint View not displaying well

我有一个约束视图作为我的 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: User 1 布局显示良好。除了没有图像外,它与 User2 类似。只有 2 篇文章

实际上 TextViews 在 ImageView 的后面!为你的 ImageView 设置这个: app:layout_constraintTop_toBottomOf="@id/chatbot_text_message_body"

根本不要使用 layout_editor_absoluteY 和 layout_editor_absoluteX,因为它们不会编译到您的应用程序中(它们在 "tools" 命名空间中) 你应该像这样改变你的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 中,但如果您有很多 TextView 和 ImageView,最好使用 CardView。