【问题标题】:How to set layout_gravity to a cardview in android dynamically如何在android中动态地将layout_gravity设置为cardview
【发布时间】:2018-11-24 23:15:13
【问题描述】:

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

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

 <?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>

【问题讨论】:

标签: android cardview layout-gravity


【解决方案1】:

将您的卡片视图包装在 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>

用户界面逻辑:

    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,但没有这样的运气,所以我按照上面的方法实现了它。

【讨论】:

    【解决方案2】:

    试试这个方法:

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

    【讨论】:

    • 它给了我错误:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    • 对不起,我没有看到这是一个RelativeLayout,检查我的udapte
    • 好吧,我在其他地方得到了答案,但无论如何还是谢谢。最好选择你的答案哈哈。
    【解决方案3】:

    我在这里得到了正确的答案:https://stackoverflow.com/a/35241091/4836759

    谢谢大家。

       RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
    cardView.setLayoutParams(layoutParams);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多