【问题标题】:Android: Dynamically add views to ScrollViewAndroid:动态添加视图到 ScrollView
【发布时间】:2013-03-31 17:54:42
【问题描述】:

我有一个 ScrollView,我想插入用户指定数量的 Horizo​​ntalScrollView。所以用户说他想要一个 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);
    }
}

【问题讨论】:

  • 你可能会遇到异常......有人说 ScrollViw 只有一个直系孩子

标签: android scrollview programmatically-created


【解决方案1】:

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

【讨论】:

  • 如果您的 RelativeLayout (或其他布局)已经是您的 ScrollView 在您的布局 XML 文件中的单个子级,那么我发现您必须这样做scrollView.removeAllViews()questionsContainer.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); scrollView.addView(relativeLayout); 否则滚动不会工作。
  • @FarrukhNajmi 你对我的问题有正确的答案,我和 Pratap 做了同样的事情,但是使用了 LinearLayout。滚动视图不起作用,您的解决方案使它起作用,谢谢!
【解决方案2】:

ScrollView 是一个单元素容器。

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

【讨论】:

  • 是的,我知道,我在 ScrollView 中有一个垂直方向的 LinearLayout。我在片段中描述不正确,我已修复它。
  • 嗯,这几乎就是我使用的代码。但我添加了确切的副本
【解决方案3】:

您在此处添加多个线性布局

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2018-03-04
    • 2011-09-07
    • 1970-01-01
    相关资源
    最近更新 更多