[英]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”方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.