附言编辑是否有可能以编程方式对齐 CardView?
我为旧的 FirebaseRecyclerAdapter 找到了一些解决方案,但它们不起作用。有什么建议吗?
我在 Java 中使用了这段代码,来自旧的 SO 帖子,但它没有影响
if (model.getReceiver() != "ADMIN")
holder.msgLinearLayout.setGravity(Gravity.LEFT | Gravity.START);
else
holder.msgLinearLayout.setGravity(Gravity.RIGHT | Gravity.END);
这是聊天 Activity 的布局,只有一个 RecyclerView 以及用于编写和发送文本和图像的底部布局。
activity_chat.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ChatActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view2"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/photoPickerButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="@android:drawable/ic_menu_gallery" />
<EditText
android:id="@+id/messageEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:enabled="false"
android:text="@string/send_button_label"/>
</LinearLayout>
</LinearLayout>
这是我为 RecyclerView 中的每个条目使用的布局。 它有一个 CardView,包含一个带有一些 TextView 的 LinearLayout。我认为问题可能出在哪里?我应该在根目录中使用一些线性布局还是相对布局?
chat_message_entry_layout.xml
<android.support.v7.widget.CardView
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"
app:cardElevation="8dp"
android:orientation="vertical"
android:id="@+id/msgCardView"
android:layout_margin="8dp">
<LinearLayout
android:id="@+id/msgLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"/>
<TextView
android:id="@+id/msg"
android:textSize="20sp"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/time"
android:backgroundTint="#0e0d0e"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
最佳答案
这是可能的,但是setGravity
不是您要找的。p>
您应该得到 LayoutParams
你的LinearLayout
并为其设置适当的重力。
FrameLayout.LayoutParams params =
(FrameLayout.LayoutParams) msgLinearLayout.getLayoutParams();
params.gravity = Gravity.LEFT;
msgLinearLayout.setLayoutParams(params);
请注意 LayoutParams
类型不是 LinearLayout.LayoutParams
但是FrameLayout.LayoutParams
.这是因为 getLayoutParams
与 msgLinearLayout
的封闭布局有关(这是 CardView
)。 CardView
正在扩展 FrameLayout
,因此是 params
的类型.
如果这对您没有帮助,我需要更多关于您的案例的数据。也许一些预期效果的插图?以及现在的样子
关于android - 使用新的 FirebaseRecyclerAdapter,如何在 android 中对齐传出和传入的文本,如 whatsapp 或 facebook messenger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50764613/