如何在Android中访问不属于我布局的视图?

时间:2016-07-14 20:18:58

标签: java android xml imageview

所以我有一个导航抽屉,抽屉的xml代码如下。 xml文件名为navigation_drawer_header。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@drawable/wallpaper">

<ImageView
    android:layout_margin="40dp"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/company_logo"
    android:id="@+id/navigation_header_logo"/>

</RelativeLayout>

然后我的活动主管在

中调用导航抽屉
<android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navigation_view"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>

您会注意到导航抽屉是在

行中实现的
app:headerLayout="@layout/navigation_drawer_header"

所以我想改变导航抽屉里面ImageView的背景,导航抽屉的id为navigation_header_logo。 我尝试过使用

ImageView navImg = (ImageView) findViewById(R.id.navigation_header_logo);
navImg.setBackgroundResource(R.drawable.new_logo);

但这似乎让应用程序崩溃了。有人可以帮忙吗?

0 个答案:

没有答案