Android如何在linearlayout中以编程方式逐行添加文本视图

Android如何在linearlayout中以编程方式逐行添加文本视图,android,android-layout,textview,android-linearlayout,Android,Android Layout,Textview,Android Linearlayout,我试图以编程方式在线性布局中添加文本视图,但问题是,当我添加5或7个以上的文本视图时,其他文本视图不会出现,因此我想在下一行添加这些文本视图,如何将这些文本视图添加到下一行。请帮助我。提前感谢 当我添加一些超过特定数量的文本视图时,它应该添加到下一行中。 我的代码如下 ArrayList<HashMap<String, String>> sel = new ArrayList<HashMap<String, String>>(); Li

我试图以编程方式在线性布局中添加文本视图,但问题是,当我添加5或7个以上的文本视图时,其他文本视图不会出现,因此我想在下一行添加这些文本视图,如何将这些文本视图添加到下一行。请帮助我。提前感谢

当我添加一些超过特定数量的文本视图时,它应该添加到下一行中。 我的代码如下

     ArrayList<HashMap<String, String>> sel = new ArrayList<HashMap<String, String>>();
 LinearLayout langaugelayout = (LinearLayout) findViewById(R.id.languagelayout);



    for (int i = 0; i < sel.size(); i++) {
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        llp.setMargins(7, 0, 0, 0);
        TextView lparams = (TextView) inflater.inflate(R.layout.textview,
                null);
        TextView tv = (TextView) lparams.findViewById(R.id.texted);
        tv.setText(sel.get(i).get("lname"));
        tv.setTypeface(Base.typeFaces.get(sel.get(i).get("id")));
        tv.setLayoutParams(llp);
        langaugelayout.addView(tv);
    }
ArrayList sel=new ArrayList();
LinearLayout langaugelayout=(LinearLayout)findViewById(R.id.languagelayout);
对于(int i=0;i
我在linearlayout中添加了这个xml如下所示

 <LinearLayout
        android:id="@+id/secondlayout"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_below="@+id/firstlayout"
        android:layout_marginBottom="10dp"
        android:layout_weight="0.05"
        android:gravity="center"
        android:orientation="horizontal" >

        <LinearLayout                 /////// adding in this layout..
            android:id="@+id/languagelayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >


        </LinearLayout>

        <TextView
            android:id="@+id/addmore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add More"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/black"
            android:textSize="25sp" />
    </LinearLayout>

在添加了一些不适合您的屏幕的内容后,它们看起来很好:-)


以下是您要查找的内容

按如下所示制作自定义流布局

public class FlowLayout extends ViewGroup {

private int line_height;

public static class LayoutParams extends ViewGroup.LayoutParams {

    public final int horizontal_spacing;
    public final int vertical_spacing;

    /**
     * @param horizontal_spacing
     *            Pixels between items, horizontally
     * @param vertical_spacing
     *            Pixels between items, vertically
     */
    public LayoutParams(int horizontal_spacing, int vertical_spacing) {
        super(0, 0);
        this.horizontal_spacing = horizontal_spacing;
        this.vertical_spacing = vertical_spacing;
    }
}

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);

    final int width = MeasureSpec.getSize(widthMeasureSpec)
            - getPaddingLeft() - getPaddingRight();
    int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop()
            - getPaddingBottom();
    final int count = getChildCount();
    int line_height = 0;

    int xpos = getPaddingLeft();
    int ypos = getPaddingTop();

    int childHeightMeasureSpec;
    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                MeasureSpec.AT_MOST);
    } else {
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
    }

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            child.measure(
                    MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
                    childHeightMeasureSpec);
            final int childw = child.getMeasuredWidth();
            line_height = Math.max(line_height, child.getMeasuredHeight()
                    + lp.vertical_spacing);

            if (xpos + childw > width) {
                xpos = getPaddingLeft();
                ypos += line_height;
            }

            xpos += childw + lp.horizontal_spacing;
        }
    }
    this.line_height = line_height;

    if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {
        height = ypos + line_height;

    } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
        if (ypos + line_height < height) {
            height = ypos + line_height;
        }
    }
    setMeasuredDimension(width, height);
}

@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(1, 1); // default of 1px spacing
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
    if (p instanceof LayoutParams) {
        return true;
    }
    return false;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    final int width = r - l;
    int xpos = getPaddingLeft();
    int ypos = getPaddingTop();

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final int childw = child.getMeasuredWidth();
            final int childh = child.getMeasuredHeight();
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (xpos + childw > width) {
                xpos = getPaddingLeft();
                ypos += line_height;
            }
            child.layout(xpos, ypos, xpos + childw, ypos + childh);
            xpos += childw + lp.horizontal_spacing;
        }
    }
}
}
公共类FlowLayout扩展了视图组{
私人内线高度;
公共静态类LayoutParams扩展了ViewGroup.LayoutParams{
公共最终int水平_间距;
公共最终int垂直_间距;
/**
*@param水平间距
*项目之间的像素,水平
*@param垂直间距
*项目之间的像素,垂直
*/
公共布局参数(int水平间距、int垂直间距){
超级(0,0);
此参数。水平间距=水平间距;
此参数。垂直间距=垂直间距;
}
}
公共流程布局(上下文){
超级(上下文);
}
公共流布局(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
断言(MeasureSpec.getMode(widthmasurespec)!=MeasureSpec.UNSPECIFIED);
最终int width=MeasureSpec.getSize(widthmasurespec)
-getPaddingLeft()-getPaddingRight();
int height=MeasureSpec.getSize(heightMeasureSpec)-getPaddingTop()
-getPaddingBottom();
最终整数计数=getChildCount();
int line_高度=0;
int xpos=getPaddingLeft();
int ypos=getPaddingTop();
国际儿童身高测量标准;
if(测量等级getMode(高度测量等级)=测量等级最多){
childHeightMeasureSpec=测量等级makeMeasureSpec(高度,
测量(最多);
}否则{
childHeightMeasureSpec=MeasureSpec.MakeMasureSpec(0,
测量(未指定);
}
for(int i=0;i宽度){
xpos=getPaddingLeft();
ypos+=线路高度;
}
xpos+=儿童w+lp.水平_间距;
}
}
this.line_height=line_height;
if(测量等级getMode(高度测量等级)=未指定的测量等级){
高度=ypos+线路高度;
}else if(MeasureSpec.getMode(heightMeasureSpec)=MeasureSpec.AT_){
if(ypos+线路高度<高度){
高度=ypos+线路高度;
}
}
设置测量尺寸(宽度、高度);
}
@凌驾
受保护的ViewGroup.LayoutParams GeneratedFaultLayoutParams(){
返回新的LayoutParams(1,1);//默认为1px间距
}
@凌驾
受保护的布尔checkLayoutParams(ViewGroup.LayoutParams p){
if(布局参数的p实例){
返回true;
}
返回false;
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
最终整数计数=getChildCount();
最终整数宽度=r-l;
int xpos=getPaddingLeft();
int ypos=getPaddingTop();
for(int i=0;i宽度){
xpos=getPaddingLeft();
ypos+=线路高度;
}
子布局(xpos、YPO、xpos+childw、YPO+childh);
xpos+=儿童w+lp.水平_间距;
}
}
}
}
并在xml中使用它,如下所示

<your_pacakge_name.FlowLayout
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#aabbcc"
    android:orientation="horizontal" >
</your_pacakge_name.FlowLayout>

简单的线性布局不会破坏它们请看这里