自定义控件getChildCount()得到总是1

qq_27490689 2016-10-27 12:21:59
以下是自定义组合控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:alpha="0.5"
android:background="@android:color/background_dark"
android:ellipsize="marquee"
android:gravity="center"
android:focusable="true"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="30px"
android:scrollHorizontally="false"
android:text="云端美食"
android:textSize="40px"
android:textStyle="bold" >

</TextView>

</RelativeLayout>
在实现类中:
public class ImageTextView extends RelativeLayout
{

public ImageTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater flater = LayoutInflater.from(context);
flater.inflate(R.layout.custom_imagetext, this);
int count=getChildCount();
}

int count=getChildCount()为什么得到的总是1,而我的控件中有两个元素。请高人指点。
...全文
373 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是路吃 2017-12-21
  • 打赏
  • 举报
回复
public int getAllChildCount(ViewGroup viewGroup){ int count =viewGroup.getChildCount(); int totalCount = count ; for(i = 0; i<count ;i++){ View child =viewGroup. getChildAt(i); if(child != null && child instanceof ViewGroup ){ totalCount += getAllChildCount(viewGroup); } } return totalCount; } 有bug,死循环了
qq_27490689 2016-10-27
  • 打赏
  • 举报
回复
引用 4 楼 u010668114 的回复:
[quote=引用 3 楼 qq_27490689 的回复:] [quote=引用 1 楼 u010668114 的回复:] getChildCount获取的是第一层子集的数量,也就是你的最外层布局RelativeLayout,所以只有一个,
有没有办法得到子节点的个数,以及遍历这些节点?[/quote] 有啊 ,你可以这样来做


public int getAllChildCount(ViewGroup viewGroup){
int count =viewGroup.getChildCount();
int totalCount = count ;
for(i = 0; i<count ;i++){
    View child =viewGroup. getChildAt(i);
    if(child != null && child instanceof  ViewGroup ){
        totalCount += getAllChildCount(viewGroup);
     }
}
return totalCount;
}
[/quote] 谢谢
只为搞笑 2016-10-27
  • 打赏
  • 举报
回复
引用 3 楼 qq_27490689 的回复:
[quote=引用 1 楼 u010668114 的回复:] getChildCount获取的是第一层子集的数量,也就是你的最外层布局RelativeLayout,所以只有一个,
有没有办法得到子节点的个数,以及遍历这些节点?[/quote] 有啊 ,你可以这样来做


public int getAllChildCount(ViewGroup viewGroup){
int count =viewGroup.getChildCount();
int totalCount = count ;
for(i = 0; i<count ;i++){
    View child =viewGroup. getChildAt(i);
    if(child != null && child instanceof  ViewGroup ){
        totalCount += getAllChildCount(viewGroup);
     }
}
return totalCount;
}
qq_27490689 2016-10-27
  • 打赏
  • 举报
回复
引用 1 楼 u010668114 的回复:
getChildCount获取的是第一层子集的数量,也就是你的最外层布局RelativeLayout,所以只有一个,
有没有办法得到子节点的个数,以及遍历这些节点?
qq840727854 2016-10-27
  • 打赏
  • 举报
回复
上面 说的 有理, 主要是 你 inflater 其实 是把 xml里面的 RelativeLayout addView 到了 你当前 的 自定义的控件里面. 所以 你想获取到RelativeLayout里面的 子控件的 话 你不妨给 RelativeLayuot加个id 然后findViewById 然后 再操作他
只为搞笑 2016-10-27
  • 打赏
  • 举报
回复
getChildCount获取的是第一层子集的数量,也就是你的最外层布局RelativeLayout,所以只有一个,

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧