导航标题显示/初始化错误

时间:2016-08-24 21:52:24

标签: android navigation-drawer android-xml android-navigation-drawer android-navigationview

我正在尝试在NavigationDrawer标题中正确显示某些用户信息,但由于某种原因,某些属性会在标题中显示两次(“某些电子邮件或其他信息”以及顶部的图像)。你能告诉我我做错了什么,需要解决。谢谢!

以下是NavigationDrawer标题的屏幕截图: NavigationDrawer header

以下是标题的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="@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");
        }
    }

1 个答案:

答案 0 :(得分:1)

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

navigationView.inflateHeaderView(R.layout.nav_header_main);

xml中的

app:headerLayout="@layout/drawer_header"。尝试禁用其中一个。

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