那位有好办法呀?现在是想在一个 view 中动态创建一些 button 组件, 如果用linearlayout 一行满后其它 button 会看不到了.
所以有什么 view 或可 content 可以实现让其它的 button 自动换到下一行显示.谢谢了.

解决方案 »

  1.   

    似乎用linearlayout没办法让button在一行吧,你确定你用的是linearlayout?这个布局管理本身就只允许一个控件在一行
      

  2.   

    谢谢!
    用其它布局也可以, 但其它的 layout 好像也没有比较简单的办法来实现这个要求吧.
      

  3.   

    谢谢!
    用这个可以, 但是也有一个问题. 如当某一个 button 的 text 很长时, 就会有同列界面空间浪费问题.
    我看到 HTC 手机中短信接收人删除 view 中有这个功能, 我和其望的比较接近, 不知哪位研究过, 共享一下经验感谢不尽!!
      

  4.   

    重写一个布局,该布局里面有N个lineLayout...可以添加BUTTON .当添加的BUTTON一行的总长度超过屏幕的宽度时。。自动增加一个lineLayout,
    把按钮添加到下一个lineLayout中
      

  5.   

    在代码中动态的添加Button可以实现吧,每次添加一个Button判断有没有超过屏幕的宽度,如果超过就增加一个LienarLayout。
      

  6.   

    我也遇到这样的问题,不过按照上面的说所得,怎么判断按钮超过屏幕的宽度呢, 
    在create Button 时 是不能用getwidth()获得按钮的宽度的啊 , 请教
      

  7.   

    什么哦   可以用滑动那个view    你把它的方向改为横向的  就可以用滑动的    这样你的Button就只有一行
      

  8.   

    没现成的。这个要在onlayout里面重新写的。而且好麻烦。还有这种不要用linearlayout吧。不推荐。如果东西不多,最好用viewgroup重写onlayout。简单点话,就用tablelayout吧。你再addview前判断上一个button右下角的坐标。我忘记函数了。反正有。然后判断和屏幕宽度的距离。然后判断需要换行不。我擦。我不确定我能写出来。越看越麻烦
      

  9.   

    还用Viewgroup吧,自己定义布局,对你的提高会更多
      

  10.   

    代码太复杂  我是直接在  Android SDK 上改的, 涉及的内容较多, 不方便贴代码
      

  11.   

    请问一下这个怎么实现的?怎么把循环产生的LinearLayout添加到主布局里?
      

  12.   

    tablelayout
    或者改android源码吧。
      

  13.   

    我提交答案吧。import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    public class LineBreakLayout extends ViewGroup
    {
    private final static String TAG = "LineBreakLayout";

    private final static int VIEW_MARGIN = 2;

    public LineBreakLayout(Context context)
    {
    super(context);
    // TODO Auto-generated constructor stub
    }

    public LineBreakLayout(Context context, AttributeSet attrs, int defStyle)
    {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    } public LineBreakLayout(Context context, AttributeSet attrs)
    {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
    //Log.d(TAG, "widthMeasureSpec = " + widthMeasureSpec+ " heightMeasureSpec" + heightMeasureSpec);
    for (int index = 0; index < getChildCount(); index++)
    {
    final View child = getChildAt(index);
    // measure
    child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4)
    {
    //Log.d(TAG, "changed = " + arg0 + " left = " + arg1 + " top = " + arg2+ " right = " + arg3 + " botom = " + arg4);
    final int count = getChildCount();
    int row = 0;// which row lay you view relative to parent
    int lengthX = arg1; // right position of child relative to parent
    int lengthY = arg2; // bottom position of child relative to parent
    for (int i = 0; i < count; i++)
    {

    final View child = this.getChildAt(i);
    int width = child.getMeasuredWidth();
    int height = child.getMeasuredHeight();
    lengthX += width + VIEW_MARGIN;
    lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
    + arg2;
    // if it can't drawing on a same line , skip to next line
    if (lengthX > arg3)
    {
    lengthX = width + VIEW_MARGIN + arg1;
    row++;
    lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
    + arg2;

    }

    child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
    }

    }

    }
      

  14.   

    怎么让里面的Button居中啊
    都是靠左的
      

  15.   

      您这样写是 固定死了 LinearLayout的高度,可不可以根据子VIew来给 LinearLayout高度。