我正在使用Firebase开发聊天应用。 我想根据用户名更改row.xml文件中的线性布局的layout_gravity。
-1
row.xml
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
}
}
};
activity_chat.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>
答案 0 :(得分:0)
试试这个
将row.xml中layout_width
的{{1}}设置为LinearLayout
,它应该可以正常工作
match_parent