android - 在 ScrollView 中连续显示 3 个按钮

标签 android button view scrollview

目前,我在 ScrollView 中显示了一个按钮列表,但是所有按钮都在左侧并在一列中向下。

我希望这些按钮显示在 3 列中,所以当我动态添加行时,一行中有 3 个按钮

这是我必须显示这些按钮的当前代码

 // Find the ScrollView
    ScrollView scrollView = (ScrollView)   
   findViewById(R.id.scrollView);

// Create a LinearLayout element
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

// Add Buttons


    for (int i = 0; i < 3; i++) {

        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));


        for (int j = 0; j < 20; j++) {
            Button button = new Button(this);
            button.setText("Some text");
            linearLayout.addView(button);
        }
    }


 // Add the LinearLayout element to the ScrollView
    scrollView.addView(linearLayout);

     }

我尝试为按钮设置布局参数,但它没有改变任何东西

有什么想法吗?

谢谢

最佳答案

试试这个逻辑

// Find the ScrollView
    ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);

// Create a LinearLayout element
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

// Add Buttons


    for (int i = 0; i < 20; i++) {
     LinearLayout linearLayoutChild = new LinearLayout(this);
     linearLayoutChild.setOrientation(LinearLayout.HORIZONTAL);
     linearLayoutChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));


      for (int j = 0; j < 3; j++) {
          Button button = new Button(this);
          button.setText("Some text");
          linearLayoutChild.addView(button);
        }
     linearLayout.addView(linearLayoutChild);
    }

 // Add the LinearLayout element to the ScrollView
    scrollView.addView(linearLayout);

除非需要,否则我会更喜欢 GridView 或 RecyclerView 而不是动态 View 添加

关于android - 在 ScrollView 中连续显示 3 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31410297/

相关文章:

android - 错误:无法初始化类org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.StaticVersionComparator

java - 错误 : Failed to resolve: androidx. annotation.annotation :1. 1.0:受影响的模块:app

javascript - 后期操作不起作用,需要帮助将信息从 View 传递到 Controller

sql - View 上的查询运行速度比直接查询慢 - 在 Oracle 中

python - Python3.1 中的 View ?

android - 如何获取两站之间的所有巴士站名称?

android - requestFocus 不工作

javascript - 如何将官方的 google +1 和 facebook 的 like/share 按钮放在同一行?

javascript - 在 jQuery 中创建元素按钮,但第二个元素不会出现

java - 如何关闭当前的扩展JFrame