相关文章推荐
千杯不醉的绿茶  ·  CMakeSettings.json ...·  1 年前    · 
微笑的水桶  ·  浅谈EditorConfig、Prettie ...·  2 年前    · 
胡子拉碴的拐杖  ·  JNI局部变量和全局变量引用_jni ...·  2 年前    · 
有胆有识的雪糕  ·  Go 语言快速入门指南:Go 转 JSON ...·  2 年前    · 
寂寞的海豚  ·  TBOX-基于CANoe实现的自动化测试方案 ...·  2 年前    · 
Code  ›  Android Studio Constraintlayout编程靠右对齐开发者社区
科技新闻 android recyclerview viewholder
https://cloud.tencent.com/developer/ask/sof/154786
火星上的猴子
1 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问
问 Android Studio Constraintlayout编程靠右对齐
Stack Overflow用户
提问于 2018-07-29 17:53:22
EN

我正在制作一个聊天应用程序,我在消息的布局上遇到了一些问题。我希望登录用户发送的消息与布局的右侧对齐,其他组成员发送的消息与左侧对齐。

这是我的xml文件:

<android.support.constraint.ConstraintLayout
android:id="@+id/container_message"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<ImageView
    android:id="@+id/user_image"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginStart="8dp"
    android:src="@color/colorBlack"
    app:layout_constraintBottom_toBottomOf="@+id/container_message_text"
    app:layout_constraintStart_toStartOf="parent" />
<TextView
    android:id="@+id/date_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:fontFamily="@font/rubik_light"
    android:text="19 juni, 00:32"
    android:textSize="10sp"
    app:layout_constraintStart_toStartOf="@+id/container_message_text"
    app:layout_constraintTop_toBottomOf="@+id/container_message_text" />
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container_message_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="48dp"
    android:paddingStart="12dp"
    android:paddingTop="12dp"
    android:paddingEnd="12dp"
    app:cardBackgroundColor="@color/colorTextWhite"
    app:cardCornerRadius="16dp"
    app:cardElevation="0dp"
    app:layout_constraintStart_toStartOf="parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingStart="12dp"
        android:paddingTop="7dp"
        android:paddingEnd="12dp"
        android:paddingBottom="7dp">
        <TextView
            android:id="@+id/message_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/rubik"
            android:textSize="16sp"
            android:maxWidth="240dp" />
    </FrameLayout>
</android.support.v7.widget.CardView>

​

当前布局的图片:

​

我需要紫色的消息向右对齐,如何通过编程来实现?

1 228 0 票数 1
EN
android
android-constraintlayout

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 19:42:06

你最好尝试聊天布局,对发送者的消息和你的消息使用两种不同的布局,如果你使用的是recyclerview,那么下面的适配器可能会对你有所帮助。

public class MessageListAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private List<UserMessage> mMessageList;
public MessageListAdapter(Context context, List<UserMessage> messageList) {
    mContext = context;
    mMessageList = messageList;
@Override
public int getItemCount() {
    return mMessageList.size();
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
    UserMessage message = (UserMessage) mMessageList.get(position);
    if (message.getSender().getUserId().equals(currentUserId)) {
        // If the current user is the sender of the message
        return VIEW_TYPE_MESSAGE_SENT;
    } else {
        // If some other user sent the message
        return VIEW_TYPE_MESSAGE_RECEIVED;
// Inflates the appropriate layout according to the ViewType
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == VIEW_TYPE_MESSAGE_SENT) {
        view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.item_message_sent, parent, false);
        return new SentMessageHolder(view);
    } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
        view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.item_message_received, parent, false);
        return new ReceivedMessageHolder(view);
    return null;
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    UserMessage message = (UserMessage) mMessageList.get(position);
    switch (holder.getItemViewType()) {
        case VIEW_TYPE_MESSAGE_SENT:
          ((SentMessageHolder) holder).bind(message);
          break;
        case VIEW_TYPE_MESSAGE_RECEIVED:
          ((ReceivedMessageHolder) holder).bind(message);
private class SentMessageHolder extends RecyclerView.ViewHolder {
    SentMessageHolder(View itemView) {
        super(itemView);
        //bind views
    void bind(UserMessage message) {
        //set values
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
    ReceivedMessageHolder(View itemView) {
        super(itemView);
        //bind views
 
推荐文章
千杯不醉的绿茶  ·  CMakeSettings.json 架构引用 | Microsoft Learn
1 年前
微笑的水桶  ·  浅谈EditorConfig、Prettier以及Eslint的使用 - 简书
2 年前
胡子拉碴的拐杖  ·  JNI局部变量和全局变量引用_jni 全局变量_文韬_武略的博客-CSDN博客
2 年前
有胆有识的雪糕  ·  Go 语言快速入门指南:Go 转 JSON - 掘金
2 年前
寂寞的海豚  ·  TBOX-基于CANoe实现的自动化测试方案(含代码) - 水一年 - 博客园
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号