在LinearLayout中添加TextView,就像与Gmail地址建议一样的芯片

Vik*_*tel 10 android android-layout

我一直在创建Chips像Gmail和大多数社交Android应用程序的地址.

我一直在追加值,LinearLayout只要它小于设备宽度就可以正常工作.一旦它的长度超过设备宽度,它就会变得混乱.

如何在每个环境中保持相同的行为?

预期行为:

预期的行为

我得到了什么

在此输入图像描述 在此输入图像描述

代码片段:

<LinearLayout
        android:id="@+id/chipsBoxLayout" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
<!--Layout to add Chips like Gmail application-->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1);
params.setMargins(5, 0, 5, 0);

Iterator<Contact> iterContacts = contacts.iterator();
while(iterContacts.hasNext()) 
{   
  Contact contact = iterContacts.next();
  TextView t = new TextView(MainActivity.this);
  t.setLayoutParams(params);
  t.setPadding(5, 5, 5, 5);
  t.setText(contact.getContactName());
  t.setTextColor(Color.WHITE);
  t.setBackgroundColor(Color.BLUE);
  chipsBoxLayout.addView(t);
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*tel 14

根据Rethinavel Pillai,

FlowLayout在添加视图时按预期工作,如果在内部添加,我将自己适应FlowLayout.

代码片段:

<com.FlowLayout
            android:id="@+id/chips_box_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start"
             >
</com.FlowLayout>
Run Code Online (Sandbox Code Playgroud)
FlowLayout chipsBoxLayout;
chipsBoxLayout = (FlowLayout)findViewById(R.id.chips_box_layout);


FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);

Iterator<Contact> iterContacts = contacts.iterator();
while(iterContacts.hasNext()) 
{   
  Contact contact = iterContacts.next();
  TextView t = new TextView(MainActivity.this);
  t.setLayoutParams(params);
  t.setPadding(5, 5, 5, 5);
  t.setText(contact.getContactName());
  t.setTextColor(Color.WHITE);
  t.setBackgroundColor(Color.BLUE);
  chipsBoxLayout.addView(t);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述