Tôi có một mảng chiều dài n, bây giờ tôi cần phải tạo n số LinearLayouts và thêm các chất liệu khác nhau trên mỗi loại. Làm thế nào nó có thể được thực hiện tự động?Cách tự động thêm LinearLayout trên Android?
5
A
Trả lời
4
LinearLayout lLayour = new LinearLayout(context);
parentWidget.addView(lLayout);
4
Cách đơn giản nhất là để tạo ra một bố cục trong xml và thổi phồng nó bằng cách sử
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
Bạn cũng có thể muốn setId()
quan điểm được trang bị để bạn có thể truy cập chúng một cách dễ dàng sau này.
2
Tôi đã giải quyết nó bằng cách sử dụng RelativeLayout mà tôi thấy dễ dàng hơn khi làm việc. Có tất nhiên như những kẻ chỉ ra ở trên tôi đã sử dụng setId()
. Đây là mã tôi đã triển khai:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
//Parent RelativeLayout
parentLayout = new RelativeLayout(this);
parentLayout.setBackgroundColor(Color.WHITE);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parentLayout.setLayoutParams(params);
sv.addView(parentLayout);
final String[] comList = getCommunication();
int listLength=0;
try{
listLength= comList.length/3;
}catch(Exception e){System.out.println(e);System.exit(0);}
childLayout= new RelativeLayout[listLength] ;
TextView[] tvName = new TextView[listLength];
TextView[] tvDate =new TextView[listLength];
TextView[] tvMsg =new TextView[listLength];
for(int i =0;i<listLength;i++){
try{
childLayout[i] = new RelativeLayout(this);
childLayout[i].setPadding(5, 5, 5, 5);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
if(i==0){params.addRule(RelativeLayout.BELOW);}
else{params.addRule(RelativeLayout.BELOW,i);}
childLayout[i].setId(i+1);
childLayout[i].setClickable(true);
childLayout[i].setLayoutParams(params);
childLayout[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Create the intent
Intent i = new Intent("ACTIIVTY");
startActivity(i);
}
});
tvName[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tvName[i].setLayoutParams(params);
childLayout[i].addView(tvName[i]);
if(comList[i*3].length()>24){
String name = comList[i*3].substring(0,24)+"...";
tvName[i].setText(name);
}else{
tvName[i].setText(comList[i*3]);
}
tvName[i].setId(listLength+1+i);
tvName[i].setTextSize(12);
tvName[i].setTextColor(Color.BLACK);
tvDate[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tvDate[i].setLayoutParams(params);
childLayout[i].addView(tvDate[i]);
tvDate[i].setTextSize(11);
tvDate[i].setTextColor(Color.BLUE);
tvDate[i].setText(comList[i*3+1]);
tvMsg[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, listLength+1+i);
tvMsg[i].setLayoutParams(params);
childLayout[i].addView(tvMsg[i]);
tvMsg[i].setTextSize(11);
tvMsg[i].setTextColor(Color.GRAY);
if(comList[i*3+2].length()>96){
String msg = comList[i*3+2].substring(0,96)+"...";
tvMsg[i].setText(msg);
}else{
tvMsg[i].setText(comList[i*3+2]);
}
parentLayout.addView(childLayout[i]);
}catch(Exception e){System.out.println("Errrorrrrr");}
}
setContentView(sv);
}
Và ở một nơi nào đó, bạn chỉ định tất cả các thuộc tính của nó (giả sử bạn cần một thứ gì đó không phải mặc định). – jwatts1980
Bạn cũng có thể đặt LayoutParams cho LinearLayout con. lLayout.setLayoutPatams (layoutParams), – ihrupin
@ihrupin cách nhận parentWidget? Tôi đang ở trong một phương thức onCreate của một lớp mà kế thừa lớp Activity và view là setContentView (R.layout.someLayout) –