Android:动态添加视图到ScrollView

4

我有一个ScrollView,想要插入用户指定数量的HorizontalScrollView。当用户说他想要一个5x5元素的矩阵时,我想插入5个每个包含5个EditText对象的HorizontalScrollView。我的程序可以像应该的那样添加第一行,但是其他行不行。

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);
    }
}

你可能会得到异常。某些内容指出 ScrollViw 只能有一个直接子元素。 - Pragnani
3个回答

5

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
如果您的RelativeLayout(或其他布局)已经是布局XML文件中ScrollView的唯一子项,则我发现必须执行scrollView.removeAllViews()questionsContainer.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); scrollView.addView(relativeLayout);否则滚动将无法正常工作。 - Farrukh Najmi
@FarrukhNajmi 你对我的问题给出了正确的答案,我做了和Pratap一样的事情,但是用的是LinearLayout。 ScrollView 不起作用,而你的解决方案使它得以工作,谢谢! - Daniel Silva

2

ScrollView是一个单元素容器。

ScrollView是一个FrameLayout,意味着您应该将一个子元素放在其中,包含要滚动的所有内容;这个子元素本身可以是一个具有复杂对象层次结构的布局管理器。经常使用的子元素是一个LinearLayout,其垂直方向排列顶级项的垂直数组,用户可以通过滚动来查看。


是的,我知道,在ScrollView中我有一个单独的LinearLayout垂直排列。我在片段中描述得不正确,我已经修复了它。 - Tomáš 'Guns Blazing' Frček
嗯,这基本上就是我正在使用的代码。但我添加了完全一样的副本。 - Tomáš 'Guns Blazing' Frček

0

你在这里添加了多个LinearLayouts

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接