效果
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="100px"
android:layout_height="100px"
android:layout_margin="20px"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="TextView" />
</LinearLayout>
package com.example.androidapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
List<Map<String,Object>> data = new ArrayList<>();
String txtList[] = new String[]{"双卡和网络","无线网络","便携式热点","蓝牙","其他连接方式",
"通知和状态栏","声音和震动","免打扰","个性化"};
int imageList[] = new int[]{R.drawable.skhwl,R.drawable.wxwl,R.drawable.bxsrd,
R.drawable.ly,R.drawable.qtljfs,R.drawable.tzhztl,
R.drawable.syhzd,R.drawable.mdr,R.drawable.gxh};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int index=0;index<txtList.length;++index){
Map<String,Object> sData = new HashMap<>();
sData.put("text",txtList[index]);
sData.put("image",imageList[index]);
data.add(sData);
}
ListView listView = findViewById(R.id.listView);
if(listView != null){
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,data,R.layout.txt_img,new String[]{"text","image"},new int[]{R.id.textView,R.id.imageView});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "按下了\""+txtList[i]+"\"", Toast.LENGTH_SHORT).show();
}
});
}
}
}
源码
扫码关注下方公众号,回复122631004,免费下载。
关注
笔者 - jxd
微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O