导航HeaderDisplay/Initialized错误

Navigation Header Display/Initialized Wrong

我试图在 NavigationDrawer header 中正确显示一些用户信息,但由于某些原因,某些属性在 header 中显示了两次("some email or other information" 和顶部的图像)。你能告诉我我做错了什么并且需要修复吗?谢谢!

这是 NavigationDrawer header 的屏幕截图:

这是 XML 的 header:

<?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="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="16dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/userImage"
    android:layout_width="96dp"
    android:layout_height="96dp"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Some user"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="some email or other information" />

这里是 java 代码的一部分,用于初始化 NaviationDrawer、加载图像等...:

main method:
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); //gets drawer button
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( //intializes toggle for navigationdrawer
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle); //sets toggle for navigationdrawer

    navigationView = (NavigationView) findViewById(R.id.nav_view); //initializes list of items in navigationdrawer
    navigationView.setNavigationItemSelectedListener(this); //sets listener for items in navigationdrawer
    navigationView.getMenu().getItem(0).setChecked(true); //sets "Map" as checked
    loadNavigationDrawerInfo(navigationView.inflateHeaderView(R.layout.nav_header_main));
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds
    drawer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Log.d("TAG", "hasFocus" + hasFocus);
        }
    });

   public void loadNavigationDrawerInfo(View headerlayout) {
        Map config = new HashMap();
        config.put("cloud_name", "some cloud name");
        config.put("api_key", "some api key");
        config.put("api_secret", "some api secret api secret");
        Cloudinary cloudinary = new Cloudinary(config);    

        new DownloadImageTask((CircleImageView) headerlayout.findViewById(R.id.userImage)).execute("some valid URL with a jpg");
}

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        CircleImageView bmImage;

        public DownloadImageTask(CircleImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            Log.d("TAG", "Done loading image");
        }
    }

当你们都使用时,这就变成了:

navigationView.inflateHeaderView(R.layout.nav_header_main);

app:headerLayout="@layout/drawer_header" 在您的 xml 中。尝试禁用其中之一。

如果您想以编程方式更改电子邮件、照片,请使用 Java 代码,如果您有稳定的 header 视图,请使用 Xml 代码。