Android 在滚动视图中一行显示3个按钮
目前,我有一个按钮列表显示在Android 在滚动视图中一行显示3个按钮,android,button,view,scrollview,Android,Button,View,Scrollview,目前,我有一个按钮列表显示在滚动视图中,但是所有按钮都在左侧,并在一列中向下 我希望这些按钮以3列的形式显示,这样当我动态添加行时,一行中有3个按钮 这是我必须显示这些按钮的当前代码 // Find the ScrollView ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); // Create a LinearLayout element LinearLayout linea
滚动视图中
,但是所有按钮都在左侧,并在一列中向下
我希望这些按钮以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);
}
//查找滚动视图
ScrollView ScrollView=(ScrollView)
findViewById(R.id.scrollView);
//创建LinearLayout元素
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.VERTICAL);
//添加按钮
对于(int i=0;i<3;i++){
linearLayout.setLayoutParams(新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
对于(int j=0;j<20;j++){
按钮按钮=新按钮(此按钮);
setText(“某些文本”);
linearLayout.addView(按钮);
}
}
//将LinearLayout元素添加到ScrollView
scrollView.addView(线性布局);
}
我试着为按钮设置布局参数,但没有改变任何东西
有什么想法吗
谢谢试试这个逻辑
// 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);
//查找滚动视图
ScrollView ScrollView=(ScrollView)findViewById(R.id.ScrollView);
//创建LinearLayout元素
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.VERTICAL);
linearLayout.setLayoutParams(新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
//添加按钮
对于(int i=0;i<20;i++){
LinearLayout linearLayoutChild=新的LinearLayout(此);
linearLayoutChild.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutChild.setLayoutParams(新的ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
对于(int j=0;j<3;j++){
按钮按钮=新按钮(此按钮);
setText(“某些文本”);
linearLayoutChild.addView(按钮);
}
linearLayout.addView(linearLayoutChild);
}
//将LinearLayout元素添加到ScrollView
scrollView.addView(线性布局);
除非需要,否则我更喜欢GridView或RecyclerView而不是动态视图添加请检查我发布的答案