package com.example.zuoyesix; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; 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 ActionBarActivity { private String[] t = { "京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫", "UC游览器","微信"}; private int[] i = { R.drawable.jd, R.drawable.qq, R.drawable.ddz, R.drawable.wb, R.drawable.tm, R.drawable.uc,R.drawable.wx }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.listView1); Myadapter md = new Myadapter(); lv.setAdapter(md); } class Myadapter extends BaseAdapter { @Override public int getCount() { return t.length; } @Override public Object getItem(int position) { return t[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 tv1=(TextView)view.findViewById(R.id.title); ImageView iv=(ImageView)view.findViewById(R.id.iv); tv1.setText(t[position]); iv.setBackgroundResource(i[position]); return view; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_margin="18dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv" android:layout_width="60dp" android:layout_height="60dp" /> <TextView android:id="@+id/Text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="35sp" android:layout_marginLeft="10dp"/> </LinearLayout> </LinearLayout>