2017-02-09 22 views
-2

Grundsätzlich lese ich Nachrichten aus Firebase-Datenbank nacheinander in einer Schleife, nach denen ich jede Nachricht dem linearen Layout hinzufügen möchte.Lineares Layout dynamisch erstellen

Ich habe eine lineare Layout-XML-Datei wie diese genannt my_linear_layout IMAGE SAMPLE

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/layout2" 
android:layout_marginTop="20dp" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_weight="6" 
    android:background="@drawable/rounded_corner" 
    android:padding="16dp" 
    android:text="MESSAGE GOES HERE. IT CAN BE A LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG MESSAGE" 
    android:textColor="#000" /> 


<ImageView 

    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_marginTop="0dp" 
    android:layout_weight="3" 
    android:src="@drawable/senduser" /> 

</LinearLayout> 

Jetzt wollen dieses Layout in meiner Aktivität des Relative-Layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" 
android:orientation="vertical" 
tools:context=".BuyerChat"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_weight="20" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 


    </RelativeLayout> 
</ScrollView> 

<include 
    layout="@layout/type_message_area" 
    android:layout_width="match_parent" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" /> 
</LinearLayout> 

ich den Code hinzufügen kann nicht einfach aufzublasen hier, weil ich n Anzahl von linearen Layouts haben möchte, abhängig davon, wie viele Nachrichten ich habe. Grundsätzlich lese ich Nachrichten aus der Firebase-Datenbank, nach denen ich jede Nachricht dem linearen Layout hinzufügen möchte.

Also, ich möchte ein Code-Snippet, mit dem ich jedes Mal ein lineares Layout hinzufügen und eine neue Nachricht jedes Mal im linearen Layout platzieren kann.

Ich bin schon seit einiger Zeit dabei, bitte helfen Sie.

+0

Was bisher versucht, Sie hatten? – Selvin

+0

@Selvin Ich habe versucht, die hier gezeigte Methode: http: //androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription & aid = 115 Aber ich weiß nicht, wie Layout und Textansicht Breite als wrap_content oder Match_Parent durch Java. – theBrainyGeek

Antwort

1

Sie können wie folgt tun:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical" 
    tools:context=".BuyerChat"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_weight="20" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:id="@+id/dynamic_holder" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin"> 


     </LinearLayout> 
    </ScrollView> 

    <include 
     layout="@layout/type_message_area" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" /> 
    </LinearLayout> 

Und in Ihrer Aktivität:

LinearLayout dynamicHolder = (LineraLayout) findViewById(R.id.dynamic_holder); 

for(int i = 0; i<your_message_length; i++){ 
    View dynamicView = LayoutInflater.from(context).inflate(R.layout.my_linear_layout, null, false); 
    TextView yourTextView = (TextView) dynamicView.findViewById(R.id.your_tv_id);//give id to your textview in my_linear_layout 
    youtTextView.setText(your_each_message_from_db); 

    dynamicHolder.addView(dynamicView); 
} 
0

Warum Sie lineares Layout statt Listenansicht verwenden.
ListView wird in diesem Fall dringend empfohlen.

Siehe here wie effizient sind listViews im Vergleich zu linearLayout.
Plus ist es einfacher zu bedienen.
ListViews blähen nicht alle Elemente auf, sie blähen auf, aber viele können auf eine Seite gleichzeitig passen. Ihre schnellste Option, mit oder ohne Paging, ist ein ListView.

Bitte lesen Sie hier, wie Sie mit dem listView für Chat-Anwendung beginnen.
Simple chat application using listview in android

Ich würde vorschlagen, dass Sie benutzerdefinierte Adapter für ListView erstellen. Sie müssen nur die Bedingung überprüfen, um den Hintergrund des Layouts/Ansichten innerhalb der Methode getViews() zu ändern.

Sie einige Thread können hilfreich sein:

The World of List View
Android Implementing Chat Bubble in ListView

Verwandte Themen