How to get Position of an view added dynamically on LinearLayout

前端 未结 4 1088
庸人自扰
庸人自扰 2020-12-12 00:39

I need to get an dynamically added view position in LinearLayout with vertical orientation. For example i have 4 TextViews added dynamically on LinearLayout, then i need to

4条回答
  •  时光取名叫无心
    2020-12-12 01:41

    If you always know the number of TextViews in your LinearLayout, you can just use the function getChildAt( int position ). This returns a View which you can then cast to a TextView to be able to perform the desired operations.

    If you do not know the number of elements you could set the id of each TextView (in order to be able to identify a particular one) and then run through them like this:

    for( View view : myLinearLayout )
      if( view instanceof TextView && view.getId().equals( idToSearchFor ) )
        //Do what needs to be done.
    

提交回复
热议问题