首页 > 解决方案 > 在android studio的水平滚动视图中添加元素

问题描述

我正在使用此代码,在将元素(动态)添加到滚动视图(水平)时遇到问题。

在最终程序中,我必须在一个垂直滚动中包含多个水平滚动(所以这只是我的应用程序代码的一部分)。

这是xml,没关系。

 <ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="390dp"
    android:layout_height="315dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.476"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    app:layout_constraintVertical_bias="0.616">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="SONO UN TITOLO" />

        <TextView
            android:id="@+id/some_text"
            android:layout_width="390dp"
            android:layout_height="match_parent"
            android:layout_below="@id/title"
            android:text="TextView" />

        <HorizontalScrollView
            android:id="@+id/scroll_h"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/some_text">

            <LinearLayout
                android:id="@+id/layout_orizzontale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            </LinearLayout>

        </HorizontalScrollView>



    </RelativeLayout>

</ScrollView>

现在在java中我有这样的代码:

LinearLayout a = (LinearLayout) findViewById(R.id.layout_orizzontale);
for(int i = 0; i < 5; i++) {
        Button tv = new Button(getApplicationContext());
        tv.setText(" hello ");
        a.addView(tv);
    }

问题是,如果我通过 xml 添加元素,它们会显示出来,但 java 代码不起作用..

标签: javaandroid

解决方案


我建议这段代码:我添加了一些注释来稍微解释一下。我已经完成了 2 个解决方案,您可以选择一个适合您接下来将要执行的添加按钮的解决方案。解决方案1:

LinearLayout a = (LinearLayout) findViewById(R.id.layout_orizzontale);
//added an ArrayList to store the Buttons if you want to use it later.
ArrayList<Button> listOfButtons = new ArrayList<Buttons>();
Button tv = new Button(getApplicationContext());
tv.setText(" hello "+0);
//added id for the rules
tv.setId(View.generateViewId());
listOfButtons.add(tv);
a.addView(tv);
for(int i = 1; i < 5; i++) {
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  Button tv = new Button(getApplicationContext());
  tv.setText(" hello "+i);
  tv.setId(View.generateViewId());
  layoutParams.addRule(LinearLayout.BELOW,listOfButtons.get(i-1).getId());
  listOfButtons.add(tv);
  a.addView(tv,layoutParams);
}

没有 ArrayList 的解决方案 2

  LinearLayout a = (LinearLayout) findViewById(R.id.layout_orizzontale);
  Button tv = new Button(getApplicationContext());
  Button tempButton;
  tv.setText(" hello "+0);
  //added id for the rules
  tv.setId(View.generateViewId());
  tempButton=tv;
  a.addView(tv);
  for(int i = 1; i < 5; i++) {
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    Button tv = new Button(getApplicationContext());
    tv.setText(" hello "+i);
    tv.setId(View.generateViewId());
    layoutParams.addRule(LinearLayout.BELOW,tempButton.getId());
    temp=tv;
    a.addView(tv,layoutParams);
  }

希望这能解决您的问题。编辑:这是我帮助理解函数 LayoutParams() developer.android的网站


推荐阅读