繁体   English   中英

在滚动视图中连续显示3个按钮

[英]Display 3 buttons in a row IN a scroll view

目前,我有一个在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而不是动态视图添加

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM