728x90
728x170
1. MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package ... import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.listView); MyAdapter adapter = new MyAdapter(this); listView.setAdapter(adapter); } } | cs |
2. MyAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package ... import android.content.Context; import android.graphics.Color; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; public class MyAdapter extends BaseAdapter { public String[] names = {"Kim", "Park", "John", "Carl", "Tim"}; public String[] ages = {"26", "23", "24", "27", "30"}; Context mContext; public MyAdapter(Context context) { mContext = context; } @Override public int getCount() { return names.length; } @Override public Object getItem(int position) { return names[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout layout = new LinearLayout(mContext); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); TextView nameView = new TextView(mContext); nameView.setText(names[position]); nameView.setTextColor(Color.BLUE); nameView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40); layout.addView(nameView, params); TextView ageView = new TextView(mContext); ageView.setText(ages[position]); ageView.setTextColor(Color.RED); ageView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24); layout.addView(ageView, params); return layout; } } | cs |
3. activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout> | cs |
4. Result
728x90
그리드형(광고전용)
'Source Code > Android' 카테고리의 다른 글
리스트뷰 (ListView) 사용 예시 2 (0) | 2017.05.09 |
---|---|
리스트뷰 (ListView) 사용 예시 1 (0) | 2017.05.09 |
프레임 레이아웃을 이용한 버튼 클릭 시 그림 전환 (0) | 2017.05.09 |
스택 (Stack) & 큐 (Queue) (0) | 2017.05.07 |
버튼을 누를 때 마다 스크롤뷰에 담겨진 레이아웃에 텍스트뷰 넣는 방법 (0) | 2017.05.06 |
화면이 터치되었을 때, X 좌표 및 Y 좌표를 알아내는 방법 (0) | 2017.05.05 |