2016-07-16 8 views
0

Ich entwickle eine Chat-App mit Firebase. Ich möchte die layout_gravity des linearen Layouts in row.xml-Datei abhängig vom Namen des Benutzers ändern.Wie layout_gravity in Firebase ändern populateView

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 

      } 
     } 
    }; 

row.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> 

Antwort

0

Versuchen Sie, diese stellen Sie Ihren layout_width für die LinearLayout in row.xml zu match_parent und es sollte funktionieren

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
params.gravity = Gravity.END; 
+0

addRule funktioniert mit RelativeLayout. Ich habe es mit RelativeLayout versucht, aber es funktioniert nicht. –

+0

Bearbeitete die Antwort. Sehen Sie, ob das funktioniert –

+0

funktioniert immer noch nicht –

Verwandte Themen