Android类微信界面布局list_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dip" >


    <TableLayout
        android:id="@+id/tl_conversation_detail_item_receive"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:visibility="gone" >


        <TableRow
            android:layout_width="0dip"
            android:layout_height="wrap_content" >


            <TextView
                android:id="@+id/tv_conversation_detail_item_receive_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/receive_msg_bubble"
                android:gravity="left|center_vertical"
                android:text="阿隆激发了多少"
                android:textColor="@android:color/black"
                android:textSize="18sp" />


            <TextView
                android:id="@+id/tv_conversation_detail_item_receive_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginBottom="3dip"
                android:layout_marginLeft="5dip"
                android:text="1990-09-09"
                android:textColor="@android:color/darker_gray"
                android:textSize="14sp" />
        </TableRow>
    </TableLayout>


    <TableLayout
        android:id="@+id/tl_conversation_detail_item_send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:visibility="gone" >


        <TableRow
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:gravity="right" >


            <TextView
                android:id="@+id/tv_conversation_detail_item_send_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginBottom="3dip"
                android:layout_marginRight="5dip"
                android:text="1990-09-09"
                android:textColor="@android:color/darker_gray"
                android:textSize="14sp" />


            <TextView
                android:id="@+id/tv_conversation_detail_item_send_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/send_msg_bubble"
                android:gravity="left|center_vertical"
                android:text="阿隆激发了多少"
                android:textColor="@android:color/black"
                android:textSize="18sp" />
        </TableRow>
    </TableLayout>


</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,以下是实现 Android仿写微信聊天界面的代码: 1. 首先,我们需要在布局文件中添加一个 RecyclerView,用于显示聊天记录: <androidx.recyclerview.widget.RecyclerView android:id="@+id/chat_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:scrollbars="vertical" app:layoutManager="LinearLayoutManager" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> 2. 然后,我们需要创建一个 ChatAdapter ,用于管理聊天记录的显示: public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<ChatMessage> mChatMessages; public ChatAdapter(List<ChatMessage> chatMessages) { mChatMessages = chatMessages; } @Override public int getItemViewType(int position) { ChatMessage chatMessage = mChatMessages.get(position); return chatMessage.getType(); } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == ChatMessage.TYPE_SEND) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send, parent, false); return new SendViewHolder(view); } else { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_receive, parent, false); return new ReceiveViewHolder(view); } } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { ChatMessage chatMessage = mChatMessages.get(position); if (holder instanceof SendViewHolder) { ((SendViewHolder) holder).mSendContentTextView.setText(chatMessage.getContent()); } else { ((ReceiveViewHolder) holder).mReceiveContentTextView.setText(chatMessage.getContent()); } } @Override public int getItemCount() { return mChatMessages.size(); } static class SendViewHolder extends RecyclerView.ViewHolder { TextView mSendContentTextView; SendViewHolder(@NonNull View itemView) { super(itemView); mSendContentTextView = itemView.findViewById(R.id.tv_send_content); } } static class ReceiveViewHolder extends RecyclerView.ViewHolder { TextView mReceiveContentTextView; ReceiveViewHolder(@NonNull View itemView) { super(itemView); mReceiveContentTextView = itemView.findViewById(R.id.tv_receive_content); } } } 3. 接下来,我们需要创建一个 ChatMessage ,用于存储聊天记录的信息: public class ChatMessage { public static final int TYPE_SEND = 0; public static final int TYPE_RECEIVE = 1; private int mType; private String mContent; public ChatMessage(int type, String content) { mType = type; mContent = content; } public int getType() { return mType; } public String getContent() { return mContent; } } 4. 最后,我们需要在 Activity 中初始化 RecyclerView 和 ChatAdapter,并添加聊天记录: public class ChatActivity extends AppCompatActivity { private RecyclerView mChatRecyclerView; private ChatAdapter mChatAdapter; private List<ChatMessage> mChatMessages = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); mChatRecyclerView = findViewById(R.id.chat_recycler_view); mChatAdapter = new ChatAdapter(mChatMessages); mChatRecyclerView.setAdapter(mChatAdapter); // 添加聊天记录 mChatMessages.add(new ChatMessage(ChatMessage.TYPE_RECEIVE, "你好,我是小明。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_SEND, "你好,我是小红。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_RECEIVE, "很高兴认识你。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_SEND, "我也很高兴认识你。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_RECEIVE, "你喜欢什么样的音乐?")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_SEND, "我喜欢流行音乐。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_RECEIVE, "我也喜欢流行音乐。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_SEND, "我们有共同的爱好。")); mChatMessages.add(new ChatMessage(ChatMessage.TYPE_RECEIVE, "是的,我们可以一起听歌。")); // 刷新聊天记录 mChatAdapter.notifyDataSetChanged(); } } 希望这份代码能够帮助到您,如果您有任何问题,请随时联系我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值