Android 从视图中获取布局

Android 从视图中获取布局,android,Android,我正在使用XML扩展接口 View add_phone = getLayoutInflater().inflate(R.layout.phone_info, null); 现在如何从add_phone视图访问RelativeLayout?是否有类似于getChildCount()的方法?是的,getChildCount()适用于诸如LinearLayout、RelativeLayout等视图组 ViewGroup add_phone = (ViewGroup) getLayoutInflate

我正在使用XML扩展接口

View add_phone = getLayoutInflater().inflate(R.layout.phone_info, null);
现在如何从add_phone视图访问RelativeLayout?是否有类似于
getChildCount()
的方法?

是的,getChildCount()适用于诸如LinearLayout、RelativeLayout等视图组

ViewGroup add_phone = (ViewGroup) getLayoutInflater().inflate(R.layout.phone_info, null);
int childCount = add_phone.getChildCount();

必须确保膨胀的布局将viewGroup作为父视图,否则将出现类强制转换异常。视图组可以是任何类似线性布局、相对视图等的内容。

您可以通过

ViewGroup add_phone = (ViewGroup) getLayoutInflater().inflate(R.layout.phone_info, null);
int childCount = add_phone.getChildCount();
View.findViewById(int id)
在你的情况下,这意味着

RelativeLayout child = (RelativeLayout)add_phone.findViewById(R.layout.phone_info) 

只要您在add_phone中有子元素的唯一id,就会返回正确的元素

请添加您的布局(phone_info.xml)以便能够帮助您:)我不明白您在说什么。添加布局是为了能够准确地告诉您如何访问它。我可以对add_phone对象使用相同的方法吗?仅当它是ViewGroup或ViewGroup的子类时。