2012-07-31 43 views
1

Hy everyone,检查一个视图是否属于线性布局

我需要一个litle的帮助。 我如何检查是否属于linearlayout的视图?

我有一个ImageButton,我需要一个条件来验证是否属于linearlayout或不。 这是一个简单的问题,但我很累,现在没有想到任何事情。我apreciate你的帮助

任何人都可以帮忙吗? 感谢您的帮助

回答

4

您可以使用上的LinearLayout的findViewById方法。从JavaDoc“查找具有给定ID的子视图,如果此视图具有给定的ID,则返回此视图。”

LinearLayout layoutWithButton = (LinearLayout)findViewById(R.id.layout_with_button); 
ImageButton buttonInLayout = (ImageButton)layoutWithButton.findViewById(R.id.button_in_layout); 

if (buttonInLayout != null) { 
    // Found 
} 
0

尝试通过使用getParent()方法

+0

嗨comodoro。感谢您的回复。这很简单,但我累了,我的大脑碰撞XDXD。 LinearLayout contentLayoutAddressPostal =(LinearLayout)findViewById(R.id.se_contentAdressPostal); ImageButton imbtRemoveAddress2 =(ImageButton)findViewById(R.id.sfsp2_ivHide);如果(imbtRemoveAddress2.getParent()== contentLayoutAddressPostal){ \t \t \t \t \t \t imbtRemoveAddress2.performClick(); – 2012-07-31 16:46:17

9

搜索UI树没有试过,但它应该工作。 假设您的ImageButton始终是您的LinearLayout的直接子对象。

View parent = (View)mContent.getParent(); 
if (parent instanceof LinearLayout) { 
    // do stuff 
} 
+0

嗨sblom和坦率。感谢您的回复。我的大脑崩溃了。 XDXD。我发表了我的解答。感谢您的帮助 – 2012-07-31 16:49:07

+0

您的解决方案告诉您您是否处于特定的ViewGroup中。这个解决方案告诉你,如果你在任何LinearLayout(这是问题:) – 2012-07-31 16:55:41

0

解决方案.....

 LinearLayout contentLayoutAddressPostal = (LinearLayout)findViewById(R.id.se_contentAdressPostal); 

    ImageButton imbtRemoveAddress2 = (ImageButton)findViewById(R.id.sfsp2_ivHide); 

     if(imbtRemoveAddress2.getParent()==contentLayoutAddressPostal){ 
      imbtRemoveAddress2.performClick(); 
            ............................ 
+0

这种解决方案只适用于如果你是直接后代。 – Frohnzie 2012-07-31 17:19:46

相关问题