如何获得动态地添加的LinearLayout的视图的位置 [英] How to get Position of an view added dynamically on LinearLayout

查看:101
本文介绍了如何获得动态地添加的LinearLayout的视图的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到与垂直方向中的LinearLayout的动态添加视图位置。
例如,我有4 TextViews上的LinearLayout动态添加,然后我需要第三的位置来改变文字颜色的位置将是在不同的color.How我可以通过获取的增加的视图位置实现它。

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 change position of text colour at 3rd position will be in different color.How can i achieve it by getting position of added views.

推荐答案

您可以做到这一点就像这样

You can do it just like that

ViewGroup parent;
    int position;

    for(int i = 0; i < parent.getChildCount(); ++i) {
        int currentViewId = parent.getChildAt(i).getId();

        if(currentViewId == wantendViewId) {
            position = i;
        }
    }

这是(在我看来)最简单的方法

That's (in my opinion) the simplest way

这篇关于如何获得动态地添加的LinearLayout的视图的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆