通过 Android 中的代码创建包含内容的动态布局数量
Create dynamic number of layouts with contents by code in Android
在我的 activity 上,我想创建一个动态数量的线性布局(在附加示例中,我尝试在 doing 循环中创建 3 个水平布局,每个包含一个文本和一个按钮,并应在父垂直线性布局中一一显示)
以下代码给出了屏幕截图中显示的结果。循环中的最后一个布局重写了所有其他布局
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(textViewParams);
textView.setText("Text number" + kkk);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 42);
linearLayout.addView(textView);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setLayoutParams(buttonParams);
button.setText("Button!" + kkk);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
linearLayout.addView(button);
setContentView(linearLayout);
}
the result of code
如屏幕上红色标记所示,我应该如何创建循环布局?
您可以创建一个新的 XML 单行 UI。并在 onClick 或方法调用时 Inflate xml。
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
text1 = (EditText) addView.findViewById(R.id.text1);
text2 = (EditText) addView.findViewById(R.id.text2);
使用循环,您将创建所有这些布局,但由于每次循环都有效,您将设置为内容视图,因为循环的最后一个布局成为父布局和您的内容视图。
在循环外,创建一个父布局并设置为内容视图。在循环内,将其他布局添加到父布局。
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.VERTICAL);
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(textViewParams);
textView.setText("Text number" + kkk);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 42);
linearLayout.addView(textView);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setLayoutParams(buttonParams);
button.setText("Button!" + kkk);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
linearLayout.addView(button);
parent.addView(linearLayout);
}
setContentView(parent);
注意:循环将运行 4 次。这意味着您将拥有 4 个相同的布局(它以 0 开头并以 3 结尾)。如果你想要 3 种布局,你应该更改范围。
for(int kkk = 0; kkk < 3; kkk++) {
}
或
for(int kkk = 1; kkk < 4; kkk++){
}
编辑
What if I need to set as a parent the layout that exists already in
xml instead of creation?
设置您的 xml,为布局创建一个实例(您想将您的子视图添加到其中)并找到它的 ID,以便您可以将它与 Java 代码一起使用。
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.idOfLayout);
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
...
parent.addView(linearLayout);
}
在我的 activity 上,我想创建一个动态数量的线性布局(在附加示例中,我尝试在 doing 循环中创建 3 个水平布局,每个包含一个文本和一个按钮,并应在父垂直线性布局中一一显示)
以下代码给出了屏幕截图中显示的结果。循环中的最后一个布局重写了所有其他布局
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(textViewParams);
textView.setText("Text number" + kkk);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 42);
linearLayout.addView(textView);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setLayoutParams(buttonParams);
button.setText("Button!" + kkk);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
linearLayout.addView(button);
setContentView(linearLayout);
}
the result of code
如屏幕上红色标记所示,我应该如何创建循环布局?
您可以创建一个新的 XML 单行 UI。并在 onClick 或方法调用时 Inflate xml。
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
text1 = (EditText) addView.findViewById(R.id.text1);
text2 = (EditText) addView.findViewById(R.id.text2);
使用循环,您将创建所有这些布局,但由于每次循环都有效,您将设置为内容视图,因为循环的最后一个布局成为父布局和您的内容视图。 在循环外,创建一个父布局并设置为内容视图。在循环内,将其他布局添加到父布局。
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.VERTICAL);
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(textViewParams);
textView.setText("Text number" + kkk);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 42);
linearLayout.addView(textView);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setLayoutParams(buttonParams);
button.setText("Button!" + kkk);
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
linearLayout.addView(button);
parent.addView(linearLayout);
}
setContentView(parent);
注意:循环将运行 4 次。这意味着您将拥有 4 个相同的布局(它以 0 开头并以 3 结尾)。如果你想要 3 种布局,你应该更改范围。
for(int kkk = 0; kkk < 3; kkk++) {
}
或
for(int kkk = 1; kkk < 4; kkk++){
}
编辑
What if I need to set as a parent the layout that exists already in xml instead of creation?
设置您的 xml,为布局创建一个实例(您想将您的子视图添加到其中)并找到它的 ID,以便您可以将它与 Java 代码一起使用。
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.idOfLayout);
for(int kkk = 0; kkk < 4; kkk++) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
...
parent.addView(linearLayout);
}