如何在NavigationView中访问RelativeLayout视图子项的按钮?但是我可以使用:
访问Header View中的TextViewsNavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header=navigationView.getHeaderView(0);
TextView tvUserName = (TextView)header.findViewById(R.id.drawer_user_name);
是否有同样的方法来访问按钮? 这是我的布局 - > Image of the layout from which i want to access buttons
这里是布局的xml:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header_main"
android:id="@+id/header"/>
<Button
android:id="@+id/btn1"
android:layout_below="@id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"
/>
<Button
android:id="@+id/btn2"
android:layout_below="@id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button
android:id="@+id/btn3"
android:layout_below="@id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"/>
<Button
android:id="@+id/btn4"
android:layout_below="@id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button4"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button5"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
答案 0 :(得分:0)
是的,你可以打电话
Button btn1 = (Button)navigationView.findViewById(R.id.btn1);
或只是
Button btn1 = (Button)findViewById(R.id.btn1);