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
<?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.
Was bisher versucht, Sie hatten? – Selvin
@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