android inflater (인플레이터) 전개 방법
안드로이드 레이아웃 동적으로 전개하기
<< 첫번째 방법 >>
private LinearLayout mWrapper; private LinearLayout mInflater; mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row = mInflater.inflate(R.layout.contents_item, null); ((TextView)row.findViewById(R.id.textTitle)).setText("제목"); ((TextView)row.findViewById(R.id.textDate)).setText("20131121"); mWrapper.addView(row);
<< 두번째 방법 >>
private LinearLayout mWrapper; View row = getLayoutInflater().inflate(R.layout.contents_item, null); ((TextView)row.findViewById(R.id.textTitle)).setText("제목"); ((TextView)row.findViewById(R.id.textDate)).setText("20131121"); mWrapper.addView(row);
<< 세번째 방법 >>
private LinearLayout mLl_Content = null; ... setContentView(R.layout.main); mLl_Content = (LinearLayout)findViewById(R.id.ll_main_content); ... mLl_Content.removeAllViews(); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View vOther = inflater.inflate(R.layout.other, null, false); mLl_Content.addView(vOther, params);
R.layout.other 는 res/layout/other.xml 파일임.
<< 네번째 방법 >>
동적으로 이미지뷰를 정해진 위치에 생성 예
private FrameLayout fl_furni_body;
private List<Map<String, String>> zoneList = new ArrayList<Map<String, String>>();
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.test_main);
mContext = this;
fl_furni_body = (FrameLayout) findViewById(R.id.fl_furni_body);
fl_furni_body.removeAllViews();
init();
addViews(zoneList.size());
}
private void init() {
zoneList.clear();
Map<String, String> map = new HashMap<String, String>();
map.put("CODE", "e975_c0101");
map.put("LEFT", "320");
map.put("TOP", "150");
zoneList.add(map);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("CODE", "e975_w0103");
map2.put("LEFT", "600");
map2.put("TOP", "300");
zoneList.add(map2);
Map<String, String> map3 = new HashMap<String, String>();
map3.put("CODE", "e975_c0501");
map3.put("LEFT", "1200");
map3.put("TOP", "300");
zoneList.add(map3);
Map<String, String> map4 = new HashMap<String, String>();
map4.put("CODE", "e975_w0301");
map4.put("LEFT", "1200");
map4.put("TOP", "600");
zoneList.add(map4);
Map<String, String> map5 = new HashMap<String, String>();
map5.put("CODE", "e975_c0401");
map5.put("LEFT", "300");
map5.put("TOP", "600");
zoneList.add(map5);
}
private void addViews(int count) {
ImageView img[] = new ImageView[count];
for (int i = 0; i < img.length; i++) {
img[i] = new ImageView(this);
img[i].setScaleType(ScaleType.FIT_CENTER);
img[i].setMaxHeight(50);
img[i].setMaxWidth(30);
img[i].setId(i);
int resId = mContext.getResources().getIdentifier(zoneList.get(i).get("CODE"), "drawable", mContext.getPackageName());
img[i].setImageResource(resId);
// img[i].setImageDrawable(getResources().getDrawable(R.drawable.e975_c0101));
img[i].setClickable(true);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = Integer.parseInt(zoneList.get(i).get("LEFT"));
params.topMargin = Integer.parseInt(zoneList.get(i).get("TOP"));
fl_furni_body.addView(img[i], params);
img[i].setTag(R.string.furni_msg_tag, zoneList.get(i).get("CODE"));
img[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = v.getTag(R.string.furni_msg_tag).toString();
Toast.makeText(mContext, code , Toast.LENGTH_SHORT).show();
}
});
}
}
'안드로이드 개발 팁' 카테고리의 다른 글
소스에서 안드로이드 layout param 설정 예제 (setLayoutParams) 및 pixel 과 dp 변환 (0) | 2013.12.04 |
---|---|
안드로이드 콜스택(call stack) 확인하는 방법 (0) | 2013.12.03 |
android interface를 이용한 상태 변동시 체크 방법 3 (0) | 2013.09.12 |
android interface를 이용한 상태 변동시 체크 방법 2 (0) | 2013.09.12 |
Context 값 얻기 (0) | 2013.09.05 |