第六个作业--android应用列表
MainActivity.java
public class MainActivity extends ActionBarActivity { // 定义数据 private String[] titles = { "京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫", "UC浏览器","微信" }; // 图片集合 private int[] icons= { R.drawable.ic_luncher,R.drawable.ic_luncher02,R.drawable.ic_luncher03,R.drawable.ic_luncher04,R.drawable.ic_luncher05,R.drawable.ic_luncher06,R.drawable.ic_luncher07}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.listView); Myadapter mAdapt = new Myadapter(); lv.setAdapter((ListAdapter) mAdapt); } class Myadapter extends BaseAdapter { public int getCount() { return titles.length; } public Object getItem(int position) { return titles[position]; } public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=View.inflate(MainActivity.this, R.layout.list_item, null); TextView tv=(TextView)view.findViewById(R.id.title); ImageView iv=(ImageView)view.findViewById(R.id.iv); tv.setText(titles[position]); iv.setBackgroundResource(icons[position]); return view; } } }
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myadapter.MainActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" > </ListView> </RelativeLayout>
list_item.xml
<?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" android:padding="16dp"> <ImageView android:id="@+id/iv" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerVertical="true"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/iv" android:layout_centerVertical="true"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="京东" android:textSize="20sp" android:textColor="#000000" /> </RelativeLayout> </RelativeLayout>