package com.example.android; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView mListView; private String[] name={"QQ斗地主","京东商城","QQ","天猫","UC浏览器","新浪微博","微信"}; private int[] img={R.drawable.doudizhu,R.drawable.jd,R.drawable.qq,R.drawable.tmall,R.drawable.uc,R.drawable.weibo,R.drawable.weixin}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView=(ListView)findViewById(R.id.lv); MyBaseAdapter mAdapter=new MyBaseAdapter(); mListView.setAdapter(mAdapter); } class MyBaseAdapter extends BaseAdapter{ @Override public int getCount() { return name.length; } @Override public Object getItem(int position) { return name[position]; } @Override 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 appname=(TextView) view.findViewById(R.id.name); ImageView appimg=(ImageView)view.findViewById(R.id.img); appname.setText(name[position]); appimg.setBackgroundResource(img[position]); return view; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<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=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > </ListView> </RelativeLayout>
<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/img" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/iv" android:layout_centerVertical="true"> </RelativeLayout> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="19dp" android:layout_toRightOf="@+id/img" android:text="QQ斗地主" android:textColor="#000000" android:textSize="20sp" /> </RelativeLayout>