LinearLayout中的许多视图行

时间:2015-10-29 16:24:18

标签: android textview android-linearlayout

如何识别设备屏幕上没有更多空间,以便在下一行添加视图。 像这样:

1]

我得到了这个:

enter image description here

使用此xml:

<LinearLayout
        android:orientation="horizontal"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tournament_client"
            android:textSize="@dimen/text_small_p"
            android:textColor="@color/grey_line"
            android:background="@color/black_10"
            android:text="Lorem Ipsum"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

我使用FlowLayout解决这个问题,正如Ertyguy所说的那样,就像魅力一样:

用XML格式:

<com.theapplabperu.hazfutbol.Utils.FlowLayout
            android:id="@+id/layout_characteristics"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_marginBottom="10dp"
            android:layout_width="match_parent"
            android:background="@color/white"
            android:orientation="vertical"
            android:layout_height="wrap_content">

像这样的循环添加视图:

for (String caracteristics : myPlayer.getCaracteristics())
                {
                    i = i+1;
                    TextView tv = new TextView(myContext);
                    tv.setText(caracteristics);
                    tv.setId(i + 20);
                    tv.setPadding(10, 10, 10, 10);
                    tv.setTextSize(14);
                    tv.setTextColor(myContext.getResources().getColor(R.color.gray_tags));
                    tv.setBackgroundDrawable(getDrawable(R.drawable.tags_shape));    
                    FlowLayout.LayoutParams layoutParams = new  FlowLayout.LayoutParams(30, 15);
                    tv.setLayoutParams(layoutParams);
                    lnrGameChars.addView(tv);
                }