private void addtext() { list = new ArrayList<>(); for (int i = 0; i< s.length; i++) { //新建textview TextView view = new TextView(this); //修改数据 view.setText(s[i]); view.setTextSize(20); if (i == 0) { view.setTextColor(Color.RED); }else{ view.setTextColor(Color.BLACK); } view.setId(i+1000); //新建点击事件 view.setOnClickListener(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(20,10,20,10); li.addView(view,params); //添加进集合 list.add(view); } } //点击事件 @Override public void onClick(View view) { //获取id int id = view.getId(); //循环遍历 for (int i=0;i<list.size();i++){ //修改颜色 if(i==id-1000){ list.get(i).setTextColor(Color.RED); vp.setCurrentItem(i); }else{ list.get(i).setTextColor(Color.BLACK); } } }
动态添加Textview+点击变色
最新推荐文章于 2023-01-13 16:11:13 发布