我正在使用Firebase开发一个聊天应用程序。我想根据用户名更改行.xml文件中线性布局的layout_gravity。
FirebaseListAdapter<ChatMessage> adapter = new FirebaseListAdapter<ChatMessage>(
this, ChatMessage.class, R.layout.row, recent
) {
@Override
protected void populateView(View view, ChatMessage chat, int i) {
((TextView) view.findViewById(R.id.nameTextView)).setText(chat.getName());
((TextView) [enter image description here][1]view.findViewById(R.id.messageTextView)).setText(chat.getMessage());
((TextView) view.findViewById(R.id.dateTextView)).setText(chat.getDate());
Log.e("PopulateView", "Called");
if (chat.getName().equals(UserName)){
// Change layout_gravity to right
}else{
// Change layout_gravity to left
}
}
};
行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_gravity="right"
android:background="@drawable/out_message_bg"
android:orientation="vertical">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:textColor="#ffffff"
android:textSize="13sp" />
<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="italic" />
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.anand.vishal.uietcommunication.ChatActivity"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/messages_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100"
android:divider="@drawable/chat_divider"
android:animateLayoutChanges="true"
android:dividerHeight="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/new_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_weight="100"
android:background="@drawable/chat_bubble_gray_new"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/type_a_message"
android:maxLines="3"
android:paddingLeft="10dp"
android:textSize="16sp" />
<Button
android:id="@+id/send_message"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:background="@drawable/ic_menu_send" />
</LinearLayout>
</LinearLayout>
试试这个将LinearLayout
的layout_width
设置为.xml match_parent
,它应该可以工作
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.gravity = Gravity.END;