首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Studio Constraintlayout编程靠右对齐

Android Studio Constraintlayout编程靠右对齐
EN

Stack Overflow用户
提问于 2018-07-29 17:53:22
回答 1查看 228关注 0票数 1

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

这是我的xml文件:

代码语言:javascript
复制
<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>

当前布局的图片:

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

EN

回答 1

Stack Overflow用户

回答已采纳

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

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

代码语言:javascript
复制
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
    }

    void bind(UserMessage message) {
        //set values
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51579130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档