Android修行之路------ListView自定义布局
主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@mipmap/bg_main" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/second_listview" android:layout_width="match_parent" android:layout_height="150dp"/> </LinearLayout>
ListView布局
<?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="match_parent"> <ImageView android:id="@+id/main_list_image" style="@style/image" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/second_text_title" style="@style/second_text_title" /> <TextView android:id="@+id/second_text_text" style="@style/second_text_text" /> </LinearLayout> </LinearLayout>
主代码
package com.example.administrator.lin_android; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class page2_main extends AppCompatActivity { //布局listview String [] textTitle=new String[]{"ETC信息查询","停车场信息查询","公交站台信息监控","小车行驶路线控制","环境监控","红绿灯控制","路灯控制"}; String [] textText=new String[]{"查询ETC费率和各个时间点小车消费记录","查询停车场费率和各个时间点小车消费记录","查询公交车站台公交车信息","设置小车行驶路径","查询各个传感器数据信息","设置每个红绿灯状态和时间","设置每个路灯的开关和自动/手动模式"}; int [] image=new int[]{R.mipmap.ip,R.mipmap.sign,R.mipmap.register,R.mipmap.ip,R.mipmap.sign,R.mipmap.register,R.mipmap.ip}; ListView second_list; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page2_main); //布局 List<Map<String,Object>> data= new ArrayList<Map<String,Object>>();
for (int i=0;i<textText.length;i++) { HashMap<String,Object> map=new HashMap<String,Object>(); map.put("textTitle",textTitle[i]); map.put("textText",textText[i]); map.put("image",image[i]); data.add(map); } SimpleAdapter adt = new SimpleAdapter(this,data,R.layout.page2_main_list, new String[]{"image","textTitle","textText"}, new int[]{R.id.main_list_image,R.id.second_text_title,R.id.second_text_text}); second_list=findViewById(R.id.second_listview); second_list .setAdapter(adt); } }