簡體   English   中英

動態創建線性布局

[英]Create Linear Layout Dynamically

基本上,我在循環中一條一條地從 firebase 數據庫讀取消息,然后我想將每條消息添加到線性布局中。

我有一個像這樣的線性布局 XML 文件,叫做 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>

現在,想在我的活動的相對布局中增加這個布局

<?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>

我不能只在此處添加代碼,因為我想要 n 個線性布局,具體取決於我有多少消息。 基本上,我從 firebase 數據庫讀取消息,然后我想將每條消息添加到線性布局中。

所以,我想要一個代碼片段,它允許我在每次調用它時添加一個線性布局,並且每次都在線性布局中放置一條新消息。

我在這已經有一段時間了,請幫忙。

你可以這樣做:

    <?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>

在您的活動中:

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);
}

你為什么使用線性布局而不是列表視圖。
在這種情況下強烈建議使用 ListView。

在這里查看與線性布局相比 listViews 的效率如何。
加上它更易於使用。
ListViews 不會膨脹所有項目,它們會膨脹,但一次可以容納許多項目。 無論是否有分頁,您最快的選擇是 ListView。

請在此處查看如何開始使用 listView 聊天應用程序。
在android中使用listview的簡單聊天應用程序

我建議您為 ListView 創建自定義適配器。 您只需要檢查條件以更改 getViews() 方法內的布局/視圖的背景。

一些線程可能對你有幫助:

列表視圖的世界
Android 在 ListView 中實現聊天氣泡

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM