Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

android - Adding TextView in LinearLayout like chips same as Gmail address suggestion

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 :

Expected Behaviour

What i got

enter image description here enter image description here

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...