<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.homework6.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" > </ListView> </RelativeLayout>
package com.example.homework6; import android.support.v7.app.ActionBarActivity; import java.util.List; import android.media.Image; 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; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private String[] name = { "京东商城", "QQ", "QQ斗地主", "新浪微博", "天猫", "UC浏览器","微信" }; private int[] picture= { R.drawable.jingdong, R.drawable.qq,R.drawable.doudizhu, R.drawable.weibo, R.drawable.tianmao,R.drawable.liulanqi,R.drawable.weixin}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.listView1); Myadapter myadapter= new Myadapter(); lv.setAdapter(myadapter); } class Myadapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return name.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return name[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view=View.inflate(MainActivity.this, R.layout.list_item, null); TextView tv=(TextView)view.findViewById(R.id.name); ImageView iv=(ImageView)view.findViewById(R.id. iv); tv.setText(name[position]); iv.setBackgroundResource(picture[position]); return view; } } }
<?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="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_toRightOf="@+id/iv" android:layout_centerVertical="true" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000" /> </RelativeLayout> </RelativeLayout>