1
A
回答
2
如果您有要在其中循環訪問的父視圖的子實例,則可以按照此實現進行操作。
您可以通過View
類型,ID或標籤進行搜索。
//layout being the child of the view you have
View parent = layout.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View v = parent.getChildAt(i);
//view type
if (v instanceof ImageView) {
//whatever
} else if (v instanceof TextView) {
//whatever
} //
//by tag
if (v.getTag() instanceof yourObject) {//or == yourObject
//whatever
} else if (v.getTag() instanceof yourOtherObject) {//or == yourOtherObject
//whatever
}
//by id
if (v.getId().equals(searchingForView.getId())) {
//whatever
} else if (v.getId().equals(searchingForOtherView.getId())) {
//whatever
}
}
但是,如果你知道ID,你能明顯做
View parent = layout.getParent();
View lookingForThisView = parent.findViewById(R.id.lookingforthisviewid);
+0
工作:)謝謝 –
+0
@KeepCalm很高興我可以幫助,如果它確實幫助你,標記爲答案! – ElliotM
1
如果你有一個家長對你的看法,你可以通過你的父視圖調用getIdentifier()
讓您的孩子的ID。如果你沒有,你可以得到它的ID爲:
getContext().getResources().getIdentifier("yourLayoutID","layout", getContext().getPackageName());
後,你如果getIdentifire()
返回0
你的佈局不存在。
1
View parent = layout.getParent();
for(int i=0;i<parent.getChildCount();i++){
View currentChild = parent.getChildAt(i);
if(currentChild.getClass() == TextView.class) {
Log.d("View Type", "TextView");
Log.d("View Type", "TextView id:" + currentChild.getId());
}
if(currentChild.getClass() == ImageView.class) {
Log.d("View Type", "ImageView");
Log.d("View Type", "ImageView id:" + currentChild.getId());
}
}
相關問題
- 1. Android多行TextView,檢查文本是否適合,或檢查是否TextView已滿
- 2. 如何從JAVA中檢查特定佈局是否包含在Android中?
- 3. TextView和ImageView的Android佈局問題
- 4. 如何檢查bitArray是否包含任何真值或任何假值?
- 5. 如何檢查一個TextView是否包含某個字符串
- 6. 如何檢查TextView是否包含數組中的字符串?
- 7. Android佈局(TextView)
- 8. 檢查是否包含jQuery
- 9. 檢查是否行包含/ *
- 10. Android的佈局「三名欄」的ImageView +的TextView + ImageView的
- 11. 在android中是否有任何類似流佈局的佈局?
- 12. 配件的ImageView和TextView的在佈局
- 13. Android - 如何檢查textview是否爲空或不爲空
- 14. 如何檢查會話對象是否包含任何對象?
- 15. 如何檢查window.localStorage是否包含任何值
- 16. 如何檢查任何矩形是否包含圓? (在pygame中)
- 17. 如何檢查字符串是否包含任何數字
- 18. 如何檢查是否輸入包含任何特殊字符
- 19. 如何檢查Django表單是否包含任何數據?
- 20. 如何檢查數組元素是否包含任何值
- 21. 如何檢查datatable是否包含任何匹配的行?
- 22. 如何檢查此/任何文件夾是否包含任何mp4文件或不在android中使用cordova?
- 23. 使用imageview線性佈局覆蓋textview
- 24. Android是有建立在ListView中包含的ImageView和TextView的
- 25. 如何檢查URL是否包含/預覽或子域是CI?
- 26. 如何檢查DataTable是否包含DataRow?
- 27. 如何檢查NSString是否包含'%'?
- 28. 如何檢查HashSet是否包含值?
- 29. 如何檢查url是否包含「main.php」
- 30. 如何檢查CGContext是否包含點?
到目前爲止您嘗試過了什麼?任何代碼? – Pavan