Добавление TextView в LinearLayout, например, такие же, как и адрес Gmail
Я создавал Chips
, как Gmail и большинство социальных приложений для Android.
Que
Я добавляю значения в LinearLayout
, работает нормально до тех пор, пока он меньше ширины устройства. Как только он будет больше, чем ширина устройства, он будет переминаться.
Как сохранить одно и то же поведение в каждом окружении?
Ожидаемое поведение:
![Expected Behaviour]()
Что я получил
![enter image description here]()
![enter image description here]()
Фрагмент кода:
<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);
}
Ответы
Ответ 1
В соответствии с 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>
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]()
Ответ 2
Вы используете LinearLayout, но Google не использует его. Линейная компоновка не будет разбивать линии и выводит все на одну строку.
Чипы в gmail работают внутри обычного textView. Он использует spannable string для вывода изображений вместо текста, или цвета фона, или smth else.
Посмотрите здесь http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/com/android/ex/chips/RecipientEditTextView.java#RecipientEditTextView
Существует код, как он работает.
Ответ 3
FlowLayout будет работать, найдите приведенный ниже URL,
https://github.com/ApmeM/android-flowlayout
@Rethinavel Pillai прав.
Ответ 4
Я думаю, проблема в том, что вы используете
android:layout_width="match_parent"
как насчет использования весовой суммы вместо??
Ответ 5
Попробуйте следующее:
вес:
Я использую это в своем приложении, и это отлично работает, оно очень полезно и вам и экономить время.
<LinearLayout
android:id="@+id/lLListImages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Button" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.35"
android:text="Button" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:text="Button" />
</LinearLayout>