在这里插入图片描述
建立一个listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:orientation="horizontal"

   android:layout_height="wrap_content">
<TextView
   android:layout_width="200dp"
   android:id="@+id/item1View"
   android:textColor="@color/black"
   android:layout_height="wrap_content">
</TextView>
<View
   android:layout_width="1dp"
   android:background="@color/black"
   android:layout_height="20dp">
</View>
   <TextView
       android:id="@+id/itemView2"
       android:layout_width="150dp"
       android:layout_height="wrap_content">
   </TextView>
   <View
   android:layout_width="1dp"
   android:background="@color/black"
   android:layout_height="20dp">
</View>
</LinearLayout>
  • 编写适配器

public class TableAdapter extends ArrayAdapter<result> {
    private int resourceId;


    public TableAdapter(@NonNull Context context, int resource, @NonNull List<result> objects) {
        super(context, resource, objects);
        resourceId=resource;
    }

    @SuppressLint("ResourceAsColor")
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        result item = getItem(position);


        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);


        TextView viewById = view.findViewById(R.id.item1View);
        TextView viewById1 = view.findViewById(R.id.itemView2);
        //判断是不是首行
        if (position==0){
            int colorid= teal_200;
            viewById.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            viewById1.setTextAlignment(view.TEXT_ALIGNMENT_CENTER);
            viewById.setBackgroundColor(colorid);
            viewById1.setBackgroundColor(colorid);
        }

        viewById.setText(item.getTime().toString());
        viewById1.setText(item.getRes());

        return view;

    }
}

public class result {
    private String time;

    private String res;


    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public result(String _time,String r){
        time=_time;

        res=r;
    }
    public String getRes() {
        return res;
    }

    public void setRes(String res) {
        this.res = res;
    }
}
  • main.xml

   <ListView
                android:id="@+id/listView"
                android:background="@color/white"
                android:layout_width="350dp"
                android:layout_height="wrap_content"> 
            </ListView>
  • main.java

 	private List<result> listData=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1actview);
        TableAdapter adapter=new TableAdapter(Test1actview.this,R.layout.table_item,listData);

        listData.add(new result("时间","异常情况"));
        listData.add(new result("2021-03-01","有人闯红灯"));

        listData.add(new result("2021-03-02","有人闯红灯"));

        ListView listviwe=findViewById(R.id.listView);
        listviwe.setAdapter(adapter);





    }
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐