2014-11-05 5 views
1

Я пытаюсь добавить кнопки в программном виде к макету, чтобы они автоматически позиционировались рядом с существующим или в новой строке, если они не подходят.Программно заполнить экран кнопками

Я не уверен, если я объяснил правильно, так положат пример результата, что я пытаюсь достичь:

Экран телефона:

|[button1][button2][button3]| 
|[sort][LongButton][sort] | 
|[tooLongSoGoesNextRow]... | 

До сих пор я» вам удалось автоматически объединить их в одну строку, но они не могут заставить их «перейти» к следующей строке, если они больше не соответствуют текущему. Я пришел из HTML, и я думал, что это будет довольно легко сделать, но как я делаю это неправильный путь, из не ...

Это, как в настоящее время мой макет выглядит следующим образом:

<LinearLayout 
     android:layout_width="fill_parent" 
     android:id="@+id/buttonsLayout" 
     android:layout_height="wrap_content"> 
</LinearLayout> 

И код, я использую, чтобы добавить кнопки:

LinearLayout layout = (LinearLayout) findViewById(R.id.buttonsLayout); 

    //set the properties for button 
    Button btnTag = new Button(getApplicationContext()); 
    btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    btnTag.setText("WHATEVER"); 
    btnTag.setId(generator.nextInt()); 

    //add button to the layout 
    layout.addView(btnTag); 

Это добавит кнопки на той же строке, не перекрывая друг друга, но они просто держать добавляются рядом друг с другом, даже если они не подходят на экране.

Любая помощь будет очень оценена.

Спасибо! Хавьер

+0

сделал и попробуйте установить ширину для линейного расположения как match_parent и высоты, как wrap_content? –

+0

Отредактировано сообщение, чтобы показать текущий макет xml, который я использую. Не пробовал с match_parent по ширине, будет делать и возвращаться. Благодаря! –

ответ

4

В конце концов, я решил его с обычаем ViewGroup (ниже этой http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html):

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 

public class FlowLayout extends ViewGroup { 

    private int paddingHorizontal; 
    private int paddingVertical; 

    public FlowLayout(Context context) { 
     super(context); 
     init(); 
    } 

    public FlowLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public FlowLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     paddingHorizontal = 0; 
     paddingVertical = 0; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int childLeft = getPaddingLeft(); 
     int childTop = getPaddingTop(); 
     int lineHeight = 0; 
     // 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout 
     int myWidth = resolveSize(100, widthMeasureSpec); 
     int wantedHeight = 0; 
     for (int i = 0; i < getChildCount(); i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() == View.GONE) { 
       continue; 
      } 
      // let the child measure itself 
      child.measure(
        getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width), 
        getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height)); 
      int childWidth = child.getMeasuredWidth(); 
      int childHeight = child.getMeasuredHeight(); 
      // lineheight is the height of current line, should be the height of the heightest view 
      lineHeight = Math.max(childHeight, lineHeight); 
      if (childWidth + childLeft + getPaddingRight() > myWidth) { 
       // wrap this line 
       childLeft = getPaddingLeft(); 
       childTop += paddingVertical + lineHeight; 
       lineHeight = childHeight; 
      } 
      childLeft += childWidth + paddingHorizontal; 
     } 
     wantedHeight += childTop + lineHeight + getPaddingBottom(); 
     setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     int childLeft = getPaddingLeft(); 
     int childTop = getPaddingTop(); 
     int lineHeight = 0; 
     int myWidth = right - left; 
     for (int i = 0; i < getChildCount(); i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() == View.GONE) { 
       continue; 
      } 
      int childWidth = child.getMeasuredWidth(); 
      int childHeight = child.getMeasuredHeight(); 
      lineHeight = Math.max(childHeight, lineHeight); 
      if (childWidth + childLeft + getPaddingRight() > myWidth) { 
       childLeft = getPaddingLeft(); 
       childTop += paddingVertical + lineHeight; 
       lineHeight = childHeight; 
      } 
      child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); 
      childLeft += childWidth + paddingHorizontal; 
     } 
    } 
} 

В макете:

<my.package.app.FlowLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/buttonsContainer"/> 

И добавление кнопок программно:

  ViewGroup flowContainer = (ViewGroup) findViewById(R.id.buttonsContainer); 
      Button btnTag = new Button(getApplicationContext()); 
      btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      btnTag.setText("whatever"); 
      btnTag.setId(23425); 
      flowContainer.addView(btnTag); 

Надеюсь, это поможет кому-то. Теперь я просто пытаюсь добавить вертикальную полосу прокрутки :).

С наилучшими пожеланиями, Хавьер

Смежные вопросы