반응형
동적인 TableRow 생성 하는 예제입니다
앞 포스트에서 http://blog.stylingandroid.com/archives/420
링크를 걸어 드렸는데 여기서 사용하는건 drawable 에 등록한 border.xml 파일과
values 에 등록한 dimensions.xml 파일이 있어야 가능한 방법입니다.
위 소스에서 4라인에서 TextView 를 배열로 생성하여 TableRow에 addview로 추가하고
마찬가지로 TableRow를 TableLayout 에 추가하는 순서로 동적으로 TableLayout에 Row가 추가되게 됩니다.
Border를 추가할때는 View객체 하나하나씩에 추가를 해줘야 되기 때문에 TextView 에
setBackgroundResource() 안에 drawable id를 입력해주시면 되겠습니다
앞 포스트에서 http://blog.stylingandroid.com/archives/420
링크를 걸어 드렸는데 여기서 사용하는건 drawable 에 등록한 border.xml 파일과
values 에 등록한 dimensions.xml 파일이 있어야 가능한 방법입니다.
public void appendRow(String[] strarray){
TableLayout tb = (TableLayout)findViewById(R.id.fav_table);
tb.removeAllViewsInLayout();
TextView[] tv = new TextView[strarray.length];
for(int i=1;i<strarray.length+1;i++){
tv[i-1] = new TextView(this);
tv[i-1].setText(strarray[i-1].toString());
tv[i-1].setTextColor(Color.parseColor("#7F7F7F"));
tv[i-1].setBackgroundResource(R.drawable.border);
tv[i-1].setGravity(Gravity.CENTER);
tv[i-1].setShadowLayer(1.0f, 1, 1, Color.parseColor("#AFFFFFFF"));
if((i!=0&&i%3==0)){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
tr.setBackgroundColor(Color.parseColor("#DFDFDF"));
tr.addView(tv[i-3], new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1));
tr.addView(tv[i-2], new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1));
tr.addView(tv[i-1], new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1));
tb.addView(tr,new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.FILL_PARENT));
}
}
}
위 소스에서 4라인에서 TextView 를 배열로 생성하여 TableRow에 addview로 추가하고
마찬가지로 TableRow를 TableLayout 에 추가하는 순서로 동적으로 TableLayout에 Row가 추가되게 됩니다.
Border를 추가할때는 View객체 하나하나씩에 추가를 해줘야 되기 때문에 TextView 에
setBackgroundResource() 안에 drawable id를 입력해주시면 되겠습니다
tv[i-1].setBackgroundResource(R.drawable.border);
반응형
'프로그래밍 > Android' 카테고리의 다른 글
| Writing exception to parcel java.lang.SecurityException: Permission Denial: .... requires that you obtain access using action_open_document or related apis 오류 (1) | 2021.06.16 |
|---|---|
| 안드로이드 : JAVA 코드에서 Drawable 설정하기 (0) | 2011.12.08 |
| 안드로이드 : Android 3.0 허니콤 소켓 개발시 주의점 (0) | 2011.12.03 |
| 안드로이드 : apk 설치시 Activity 마다 아이콘이 생기는 경우 (0) | 2011.12.02 |
| 안드로이드 : TableLayout - TableRow 꾸미기, Style 주기 (0) | 2011.12.01 |