如何动态地将 layout_gravity 设置为 android 中的卡片视图

How to set layout_gravity to a cardview in android dynamically

我正在尝试在我的应用程序中创建类似聊天的功能,并且我正在使用 cardview 来创建聊天文本。我希望卡片视图与发送消息的屏幕右侧对齐。但是当我将重力属性设置为 cardview 的父级时,它适用于所有其他属性,但 cardview 不会让步。请告诉我如何将 layout_gravity 设置为 cardview(将应用动态方法)。谢谢

这是我的 model_msg.xml 文件,发送消息的设置在适配器中完成 class:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_marginEnd="40dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:layout_height="wrap_content"
    android:id="@+id/rl_msg"
    android:layout_gravity="center">

        <TextView
            android:paddingStart="8dp"
            android:text="Sender Name"
            android:maxLines="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_senderName"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true" />

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            card_view:cardBackgroundColor="@color/cBgrMsg"
            card_view:cardElevation="6dp"
            android:layout_height="wrap_content"
            android:id="@+id/card_msgText"
            android:layout_below="@id/tv_senderName"
            card_view:cardCornerRadius="6dp">

        <TextView
        android:text="The message will come here which is gonna be a lot larger than this textView can handle and at that time to avoid any bugs or not to discard and functionalities I would have to utilize my mind a lot which is gonna be so tiresome and annoying that I  would think about leaving this app in middle without finishing it. Damn."
        android:padding="6dp"
        android:textSize="16sp"
        android:maxLength="500"
        android:layout_width="wrap_content"
        android:textColor="@color/cWhite"
        android:layout_height="wrap_content"
        android:id="@+id/tv_msg"
        android:layout_below="@id/tv_senderName" />

        </android.support.v7.widget.CardView>

    <TextView
        android:text="Nothing to show."
        android:padding="4dp"
        android:background="#0000"
        android:textSize="10sp"
        android:textStyle="italic"
        android:layout_width="wrap_content"
        android:gravity="start"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:textColor="#999999"
        android:layout_height="wrap_content"
        android:id="@+id/tv_sentAt"
        android:layout_below="@id/card_msgText" />
</RelativeLayout>

试试这个方法:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
yourCardViewHere.setLayoutParams(params);

我在这里得到了正确的答案伙计们:

谢谢大家。

   RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
cardView.setLayoutParams(layoutParams);

在 LinearLayout 中包装您的卡片视图。然后在 LinearLayout 上使用 setGravity(Gravity gravity) 设置线性布局的重力,如下所示。请注意,我正在使用 RecyclerView 来重复消息。

message_row_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/message_view">

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/message_text_view" />
        </android.support.v7.widget.CardView>
    </LinearLayout>

UI逻辑:

    LinearLayout viewMessage = itemView.findViewById(R.id.message_view);

    if (messageList.get(position).getPeerId() == USER_PEER_ID) {
        viewMessage.setGravity(LEFT);
    } else {
        viewMessage.setGravity(RIGHT);
    }

注:

我尝试按照其他答案的指示访问 CardView 的 LayoutParams 和 cardview 上的 setForegroundGravity,但没有这样的运气,所以我按照上面的方式实现了它。