Android 无法访问ViewStub';小孩

Android 无法访问ViewStub';小孩,android,android-layout,viewstub,Android,Android Layout,Viewstub,我正在尝试在合并标记内使用VIEWSTUB。它工作正常。我能够捕获VIEWSTUB的父按钮的onclicklistenr。但我想访问VIEWSTUB内的按钮 <Button android:id="@+id/button_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="100dip" android:text="Next" /> <

我正在尝试在合并标记内使用VIEWSTUB。它工作正常。我能够捕获VIEWSTUB的父按钮的onclicklistenr。但我想访问VIEWSTUB内的按钮

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>
1.Main xml:

<merge>
<LinearLayout>
<Button></Button>
<ViewStub></ViewStub>
</LinearLayout>
</merge>
<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>

2.查看存根布局

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>


我正在一个活动中膨胀视图存根…这里我想在按钮取消时触发单击事件。这将如何可能

让我们假设您的视图存根ID是视图存根。您需要在活动中执行以下操作:

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
View inflatedView = viewStub.inflate();
Button button = (Button) inflatedView.findViewById(R.id.button_cancel);
现在,您可以使用按钮执行任何操作:)也就是说,充气方法返回存根布局,其中包含XML文件中的实际元素

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>
当然,您可以始终使用onClick XML属性

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>
关于移除ViewStub,问题有两个(勾选):

<Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:minWidth="100dip"
android:text="Next" />
<ImageView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:background="@drawable/golden_gate"
/>
</LinearLayout>
  • 在ViewStub膨胀之前-您不能实际移除它。不过,没有必要,因为ViewStub“没有维度,它不绘制任何东西,也不以任何方式参与布局”

  • <Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:minWidth="100dip"
    android:text="Next" />
    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:background="@drawable/golden_gate"
    />
    </LinearLayout>
    
  • 充气后-您只需获取ViewStub.inflate()方法返回的视图,并对其执行任何操作-例如,隐藏它

  • <Button android:id="@+id/button_cancel" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:minWidth="100dip"
    android:text="Next" />
    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:background="@drawable/golden_gate"
    />
    </LinearLayout>
    

Plz单击editThank Kamen.查看完整代码。如果我想从屏幕上动态删除存根,如何删除它?