Android:动态地向ScrollView添加视图

时间:2013-03-31 17:54:42

标签: android scrollview programmatically-created

我有一个ScrollView,我想插入一个用户指定数量的Horizo​​ntalScrollViews。那么用户说他希望有一个5x5元素的矩阵,我想插入5个Horizo​​ntalScrollViews,每个包含5个EditText对象。我的程序按照预期添加了第一行,但其余部分没有。

for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(par2);
        HorizontalScrollView row = new HorizontalScrollView(this);
        row.setLayoutParams(par1);
        row.addView(ll);
        for (int j = 0; j < number; j++) {
            EditText txt = new EditText(this);
            txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            txt.setHint(i+","+j);
            ll.addView(txt);
        }
        latout_in_scrollview.addView(row);
    }

任何想法为什么?谢谢!

编辑: 使用

的1:1代码
LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_dijkstra);

    dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);

    Bundle extras = getIntent().getExtras();
    number = extras.getInt("vertexes");

    for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(par2);
        HorizontalScrollView row = new HorizontalScrollView(this);
        row.setLayoutParams(par1);
        row.addView(ll);
        for (int j = 0; j < number; j++) {
            EditText txt = new EditText(this);
            txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            txt.setHint(i+","+j);
            ll.addView(txt);
        }
        dijkstra_rows.addView(row);
    }
}

3 个答案:

答案 0 :(得分:4)

ScrollView只能包含一个childView。您可以根据您的要求放置任何布局。我通常使用相对布局......

然后动态地将视图添加到相对布局

 viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
        View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);

    // INFLATE YOUR NEW VIEW YOU WANT TO ADD
                CardView cardView = (CardView) 

LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            //Set id to view
            int id = 125;
            if (lastCard != null) {
                params.addRule(RelativeLayout.BELOW, lastCard.getId());
                id = lastCard.getId() + 125;
            }
            cardView.setLayoutParams(params);
            cardView.setId(id);
            viewLayout.addView(cardView);

答案 1 :(得分:1)

ScrollView是单个元素容器。

  

ScrollView是一个FrameLayout,意味着你应该放置一个孩子   它包含滚动的全部内容;这个孩子本身可能是   具有复杂对象层次结构的布局管理器。一个孩子   经常使用的是垂直方向的LinearLayout,呈现一个   用户可以滚动的顶级项目的垂直数组。

答案 2 :(得分:0)

您在此处添加了多个LinearLayouts

 for (int i = 0; i < number; i++) {
        LinearLayout ll = new LinearLayout(this);
.
.
}

你应该只有一个这个循环。然后将这个添加到您的scrollView,在循环中,您可以将多个Horizo​​ntolScrollViews添加到此LinearLayout