相对布局 android 可循环
Relative layout android loopable
我正在编写一个应用程序,它为找到的每个文件加载一行按钮(2 个按钮,然后是下面的黑线)(因此循环计数不会是静态的)。目前在构建时我有一个静态循环计数为 15。但是当 运行 代码时,它在左侧创建了更大的按钮,下面的黑线很好......但是......右侧的较小按钮只出现一次。知道为什么吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout appLayout = new RelativeLayout(this);
// appLayout.setClipBounds(null);
Resources r = getResources();
ImageView blackLine;
RelativeLayout.LayoutParams p;
int id = 1;
for(int x = 1; x <= 15; x++){
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button studentsButton = new Button(this);
studentsButton.setClipBounds(null);
studentsButton.setId(id);
studentsButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
studentsButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
//studentsButton.setBackgroundColor(Color.LTGRAY);
if (x > 1 ){
p.addRule(RelativeLayout.BELOW, id - 1);
studentsButton.setLayoutParams(p);
}
appLayout.addView(studentsButton);
id ++;
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button soundButton = new Button(this);
soundButton.setClipBounds(null);
soundButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
soundButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
//soundButton.setBackgroundColor(Color.LTGRAY);
p.addRule(RelativeLayout.RIGHT_OF, id - 1);
soundButton.setLayoutParams(p);
appLayout.addView(soundButton);
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
blackLine = new ImageView(this);
blackLine.setId(id);
blackLine.setBackgroundColor(Color.BLACK);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setMinimumHeight(3);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setClipBounds(null);
p.addRule(RelativeLayout.BELOW, id - 1);
blackLine.setLayoutParams(p);
appLayout.addView(blackLine);
id++;
}
scrollPictures.addView(appLayout);
setContentView(scrollPictures);
}
老实说,用代码来完成这一切似乎有些矫枉过正。我建议创建一个布局文件,并多次膨胀它。将 appLayout 设置为垂直方向的 LinearLayout,就这么简单:
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);
// Configure the items inside the view
Button button1 = (Button)view.findViewById(R.id.button1);
button1.setText("Button 1");
button1.setOnClickListener(new View.OnClickListener() { ... });
...
appLayout.addView(view);
}
而且您的布局文件也相对简单:
<LinearLayout android:orientation="vertical" ...>
<LinearLayout android:orientation="horizontal" ...>
<Button android:id="@+id/button1" ... />
<Button android:id="@+id/button2" ... />
</LinearLayout>
<View android:background="@android:color/black"
android:layout_height="3dp"
android:layout_width="match_parent" />
</LinearLayout>
虽然在代码中布置元素绝对可行,但要做到正确可能会令人头疼。我发现使用 XML 文件更容易,并根据需要像这样扩充它们。我的猜测是您使用 ID 的方式混淆了布局。最好让按钮获得自己的 ID(不要使用 setId),并在设置相对布局参数时使用 "getId" 方法。
我正在编写一个应用程序,它为找到的每个文件加载一行按钮(2 个按钮,然后是下面的黑线)(因此循环计数不会是静态的)。目前在构建时我有一个静态循环计数为 15。但是当 运行 代码时,它在左侧创建了更大的按钮,下面的黑线很好......但是......右侧的较小按钮只出现一次。知道为什么吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollPictures = new ScrollView(this);
RelativeLayout appLayout = new RelativeLayout(this);
// appLayout.setClipBounds(null);
Resources r = getResources();
ImageView blackLine;
RelativeLayout.LayoutParams p;
int id = 1;
for(int x = 1; x <= 15; x++){
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button studentsButton = new Button(this);
studentsButton.setClipBounds(null);
studentsButton.setId(id);
studentsButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
studentsButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
//studentsButton.setBackgroundColor(Color.LTGRAY);
if (x > 1 ){
p.addRule(RelativeLayout.BELOW, id - 1);
studentsButton.setLayoutParams(p);
}
appLayout.addView(studentsButton);
id ++;
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button soundButton = new Button(this);
soundButton.setClipBounds(null);
soundButton.setHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
soundButton.setWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()));
//soundButton.setBackgroundColor(Color.LTGRAY);
p.addRule(RelativeLayout.RIGHT_OF, id - 1);
soundButton.setLayoutParams(p);
appLayout.addView(soundButton);
p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
blackLine = new ImageView(this);
blackLine.setId(id);
blackLine.setBackgroundColor(Color.BLACK);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setMinimumHeight(3);
blackLine.setMinimumWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 700, r.getDisplayMetrics()));
blackLine.setClipBounds(null);
p.addRule(RelativeLayout.BELOW, id - 1);
blackLine.setLayoutParams(p);
appLayout.addView(blackLine);
id++;
}
scrollPictures.addView(appLayout);
setContentView(scrollPictures);
}
老实说,用代码来完成这一切似乎有些矫枉过正。我建议创建一个布局文件,并多次膨胀它。将 appLayout 设置为垂直方向的 LinearLayout,就这么简单:
for(int x = 1; x <= 15; x++){
View view = LayoutInflater.from(this).inflate(R.layout.some_layout, appLayout, false);
// Configure the items inside the view
Button button1 = (Button)view.findViewById(R.id.button1);
button1.setText("Button 1");
button1.setOnClickListener(new View.OnClickListener() { ... });
...
appLayout.addView(view);
}
而且您的布局文件也相对简单:
<LinearLayout android:orientation="vertical" ...>
<LinearLayout android:orientation="horizontal" ...>
<Button android:id="@+id/button1" ... />
<Button android:id="@+id/button2" ... />
</LinearLayout>
<View android:background="@android:color/black"
android:layout_height="3dp"
android:layout_width="match_parent" />
</LinearLayout>
虽然在代码中布置元素绝对可行,但要做到正确可能会令人头疼。我发现使用 XML 文件更容易,并根据需要像这样扩充它们。我的猜测是您使用 ID 的方式混淆了布局。最好让按钮获得自己的 ID(不要使用 setId),并在设置相对布局参数时使用 "getId" 方法。