獲取listview中的子元素view, 設置textview的圖標:setCompoundDrawables使用, view.textView.getHeight()爲0


(只能獲取當前可見的子元素view)

int firstVisableIndex = listView.getFirstVisiblePosition();

 int index = busstations.indexOf(targetStation); //獲取目標元素targetStation在list(busstations)中的索引位置

 TextView tempView = (TextView)(listView.getChildAt(index - firstVisableIndex));//(index - firstVisableIndex爲目標元素targetStation在當前可見views中的索引位置


setCompoundDrawables使用:

Drawable drawable = getResources().getDrawable(R.drawable.clock);
drawable.setBounds(0, 0, textView.getHeight(), textView.getHeight());//後兩個參數爲圖標的高寬
 textView.setCompoundDrawables(  null,null, drawable, null);//設置右圖標


//隱藏右圖標

Drawable drawable = getResources().getDrawable(R.drawable.clock);
drawable.setBounds(0, 0, 0, 0);
 textView.setCompoundDrawables(  null,null, drawable, null);
text1.setCompoundDrawables(  null,null, drawable, null);


在oncreate中取view.textView.getHeight()爲0:

據說是在此方法調用完成前,view還沒有被繪製,因而取不到height


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章