为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过活动的 ID 在导航视图中找到视图?

如何通过活动的 ID 在导航视图中找到视图?

斯蒂芬大帝 2022-10-26 16:27:21
我有一个活动布局,它调用另一个 xml 文件作为标题。我想通过 id 中设置的图像视图的 id 获取参考nav_header_main,但我无法访问它。活动主:<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:openDrawer="start">  <android.support.design.widget.NavigationView    android:id="@+id/nav_view"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_gravity="start"    android:fitsSystemWindows="true"    app:headerLayout="@layout/nav_header_main"    app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>nav_header_main:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="@dimen/nav_header_height"    android:background="@drawable/side_nav_bar"    android:gravity="bottom"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:theme="@style/ThemeOverlay.AppCompat.Dark">请注意,nav_header_main在activity_layout.我试图在活动中设置一个 OnClickListener,但我得到一个 NullException,只是因为我无法从活动中访问该 ImageButton 视图。logoImageButton = FindViewById<ImageButton>(Resource.Id.LogoButtonView); //Returns nulllogoImageButton.SetOnClickListener(this);有什么办法可以绕过它并获得参考吗?好像我错过了一件简单的事情。
查看完整描述

2 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View hView =  navigationView.getHeaderView(0); TextView nav_user = (TextView)hView.findViewById(R.id.nav_name); nav_user.setText(user);

查看完整回答
反对 回复 2022-10-26
?
白衣非少年

TA贡献1155条经验 获得超0个赞

nav_draw.getMenu().findItem(R.id.LogoButtonView).setVisible(false);



查看完整回答
反对 回复 2022-10-26
  • 2 回答
  • 0 关注
  • 162 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号