2017-02-09 10 views
-2

基本的には、ファイアベースデータベースからメッセージを1つずつループで読み込み、各メッセージを線形レイアウトに追加します。動的に線形レイアウトを作成する

私は、 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> 

今、この呼ばmy_linear_layoutようなリニアレイアウトXMLファイルを持っている

<?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データベースからメッセージを読んだ後、各メッセージを線形レイアウトに追加したいと思います。

私はそれを呼び出すたびにリニアレイアウトを追加し、毎回リニアレイアウトで新しいメッセージを配置できるコードスニペットが必要です。

私はしばらくお待ちしておりますので、助けてください。

+0

は、あなたがこれまでにしようとしましたか? – Selvin

+0

@Selvinここに示す方法を試しました:http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view = article_discription&aid = 115 しかし、レイアウトとテキストビューの幅をwrap_contentとして設定する方法がわかりません。 match_parentからjava。 – theBrainyGeek

答えて

1

あなたは次のように行うことができます。

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

は、なぜあなたは、リニアレイアウトの代わりに、リストビューを使用しています。
この場合、ListViewを強くお勧めします。

hereを参照してください。listViewsとlinearLayoutを比較すると効率的です。
さらに使いやすくなっています。
ListViewはすべてのアイテムを膨らませませんが、一度に1ページに収まるように多くのアイテムを膨張させます。ページングの有無にかかわらず、最も速いオプションはListViewです。

ここをクリックしてチャットアプリケーションのlistViewを開始してください。
Simple chat application using listview in android

ListViewのカスタムアダプタを作成することをお勧めします。 getViews()メソッド内のレイアウト/ビューの背景を変更するには、条件をチェックするだけです。

いくつかのスレッドがあなたに役立つかもしれない:

The World of List View
Android Implementing Chat Bubble in ListView

関連する問題