两个ImageView出现在Navigation标题中,而我只在xml中设置了一个

Joe*_*Min 3 xml android view imageview navigation-drawer

所以我遇到了一种非常奇怪的行为.我有一个NavigationDrawer和里面我有我的导航标题,它有一个Facebook ProfilePictureView和一个TextView.

问题是,当我运行我的应用程序时,导航标题中有两个ProfileImageViews.即使在Android Studio中,标题xml的预览也会ProfileImageView正确显示.

我在下面附上了截图:

在此输入图像描述

这是我的nav_header_home.xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@color/dark_blue"
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">

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/profile_image"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_gravity="center"
    android:id="@+id/tv_username"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:textColor="@color/white" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这就是我初始化它的方式:

View headerView = navigationView.inflateHeaderView(R.layout.nav_header_home);
usernameTextView = (TextView)headerView.findViewById(R.id.tv_username);
profileImage = (ProfilePictureView)headerView.findViewById(R.id.profile_image);
Run Code Online (Sandbox Code Playgroud)

这是另一种可能有助于解决问题的奇怪行为:

当我这样初始化时ProfilePictureView:

profileImage = (ProfilePictureView)findViewById(R.id.profile_image);

然后将图像设置为视图,顶部的视图显示图像.

如果我像这样初始化:

profileImage = (ProfilePictureView)headerView.findViewById(R.id.profile_image);

然后底部的视图显示图像.

Joe*_*Min 6

好吧,我发现了问题.这条线View headerView = navigationView.inflateHeaderView(R.layout.nav_header_home);似乎没有得到当前的信号header,相反它似乎膨胀并创建一个新的,这可能是我ProfilePictureView被添加两次的原因!

View headerView = navigationView.getHeaderView(0);作品代替它!它正确获取当前标题!