Android 检查视图是否属于线性布局

Android 检查视图是否属于线性布局,android,android-layout,android-linearlayout,android-view,Android,Android Layout,Android Linearlayout,Android View,我需要一点帮助。 如何检查视图是否属于线性布局 我有一个ImageButton,我需要一个条件来验证是否属于linearlayout。尝试通过getParent()方法搜索UI树。没有尝试过这个方法,但应该可以。 假设ImageButton始终是线性布局的直接子级 View parent = (View)mContent.getParent(); if (parent instanceof LinearLayout) { // do stuff } 解决办法 LinearL

我需要一点帮助。 如何检查视图是否属于线性布局


我有一个ImageButton,我需要一个条件来验证是否属于linearlayout。

尝试通过getParent()方法搜索UI树。

没有尝试过这个方法,但应该可以。 假设ImageButton始终是线性布局的直接子级

View parent = (View)mContent.getParent();
if (parent instanceof LinearLayout) {
    // do stuff
}
解决办法

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

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

       if(imbtRemoveAddress2.getParent()==contentLayoutAddressPostal){
          imbtRemoveAddress2.performClick();
                                    ............................
可以在LinearLayout上使用该方法。从JavaDoc“查找具有给定id的子视图。如果此视图具有给定id,则返回此视图。”


嗨,科摩多罗。谢谢你的回复。这很简单,但我很累,我的大脑崩溃了。LinearLayout contentLayoutAddressPostal=(LinearLayout)findViewById(R.id.se_ContentAddressPostal);ImageButton imbtRemoveAddress2=(ImageButton)findViewById(R.id.sfsp2_ivHide);if(imbtRemoveAddress2.getParent()==contentLayoutAddressPostal){imbtRemoveAddress2.performClick();您好,sblom和frank。感谢您的回复。我的大脑崩溃。XDXD。我发布了我的解决方案。感谢您的帮助。您的解决方案会告诉您是否在特定的视图组中。此解决方案会告诉您是否在任何线性布局中(问题:)
LinearLayout layoutWithButton = (LinearLayout)findViewById(R.id.layout_with_button);
ImageButton buttonInLayout = (ImageButton)layoutWithButton.findViewById(R.id.button_in_layout);

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