添加的TextView的LinearLayout中像芯片一样的Gmail地址建议芯片、地址、建议、TextView

2023-09-05 11:00:30 作者:假如你爹是广坤i

我已经创建芯片如Gmail和大多数社交Android应用程序的地址。

我一直在追加的LinearLayout 值是工作的罚款,只要它小于设备宽度。只要它的长度超过设备宽度它变得混杂起来。的

如何在每一个enviornment一个preserve相同的行为?的

预期的行为:的

我得到了什么的

code段:

 <的LinearLayout
        机器人:ID =@ + ID / chipsBoxLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=横向
        >
<! - 布局增添筹码,如Gmail应用程序 - >
< / LinearLayout中>
 

  LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);
params.setMargins(5,0,5,0);

迭代器<联系与GT; iterContacts = contacts.iterator();
而(iterContacts.hasNext())
{
  联系方式联系方式= iterContacts.next();
  TextView的T =新的TextView(MainActivity.this);
  t.se​​tLayoutParams(PARAMS);
  t.se​​tPadding(5,5,5,5);
  t.se​​tText(contact.getContactName());
  t.se​​tTextColor(Color.WHITE);
  t.se​​tBackgroundColor(Color.BLUE);
  chipsBoxLayout.addView(T);
}
 

解决方案

根据Rethinavel皮莱,

2.2.1 LinearLayout 线性布局

的FlowLayout 将按预期在增加的看法,我会自己,如果它里面的的FlowLayout 。

code段:

 < com.FlowLayout
            机器人:ID =@ + ID / chips_box_layout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:重力=开始
             >
< /com.FlowLayout>
 

 的FlowLayout chipsBoxLayout;
chipsBoxLayout =(的FlowLayout)findViewById(R.id.chips_box_layout);


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

迭代器<联系与GT; iterContacts = contacts.iterator();
而(iterContacts.hasNext())
{
  联系方式联系方式= iterContacts.next();
  TextView的T =新的TextView(MainActivity.this);
  t.se​​tLayoutParams(PARAMS);
  t.se​​tPadding(5,5,5,5);
  t.se​​tText(contact.getContactName());
  t.se​​tTextColor(Color.WHITE);
  t.se​​tBackgroundColor(Color.BLUE);
  chipsBoxLayout.addView(T);
}
 

I've been creating Chips like Gmail and most of the social android application for address.

Que

I've been appending values in LinearLayout is working fine as long as it less than device width. As soon as it's length more than device width it gets jumble up.

How can a preserve same behaviour in every enviornment?

Expected Behaviour :

What i got

Code Snippet:

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

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

解决方案

As per Rethinavel Pillai ,

FlowLayout works as expected in adding views which i will accomodate by itself if it's added inside FlowLayout.

Code Snippet:

<com.FlowLayout
            android:id="@+id/chips_box_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start"
             >
</com.FlowLayout>

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