如何从另一个视图中获取View

时间:2013-07-24 09:05:12

标签: java android

这是我的xml文件

             <FrameLayout 
                 android:layout_alignParentRight="true"
                 android:layout_alignParentBottom="true"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 >
                 <Button
                    android:id="@+id/button1"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:text="@string/productButton_download"
                    android:onClick="testGetView"
                     />

                 <ProgressBar
                     style="?android:attr/progressBarStyleHorizontal"
                     android:layout_width="50dp"
                     android:layout_height="30dp"
                     android:visibility="invisible"
                      />
             </FrameLayout>

我想让ProgressBar实例使用函数testGetView而不是让它使用资源ID

public void testGetView(View button) {
    // what can I do use this view to get the progressbar below it

}

1 个答案:

答案 0 :(得分:1)

您必须为ProgressBar设置一个ID,然后从您的按钮,使用getParent()获取其父视图,将其转换为ViewGroup,然后使用findViewById(int id)调用ProgressBar您{{1}}

的ID
相关问题