<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" tools:context=".MainActivity"> <ListView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/jd" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" > <TextView android:id="@+id/name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="京东商城" android:textSize="50sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>
package cn.itcase.android; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ListView; import android.widget.BaseAdapter; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private ListView mListView; private String[] titles ={"京东商城","QQ","QQ斗地主","新浪微博","天猫商城","UC浏览器","微信"}; private int[] icons ={R.drawable.jd,R.drawable.qq,R.drawable.ddz,R.drawable.wb,R.drawable.tm,R.drawable.ll,R.drawable.wx}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView =(ListView) findViewById(R.id.iv); MyBaseAdapter mAdapter =new MyBaseAdapter(); mListView.setAdapter(mAdapter); } class MyBaseAdapter extends BaseAdapter{ public int getCount(){ return titles.length; } public Object getItem(int posision){ return titles[posision]; } public long getItemId(int position){ return position; } public View getView(int position, View convertView, ViewGroup parent){ View view =View.inflate(MainActivity.this,R.layout.list_layout,null); TextView title =(TextView) view.findViewById((R.id.name1)); title.setText(titles[position]); ImageView lv=(ImageView)view.findViewById(R.id.jd); lv.setBackgroundResource(icons[position]); return view; } } }