Android导航抽屉从URL设置图像

时间:2016-11-12 15:27:08

标签: android navigation drawer

我正在尝试使用导航抽屉构建应用。我需要的是能够通过滑动从代码中动态改变导航抽屉头部的图像。放完这段代码后,我得到两次导航标题就像在图片上一样。如何解决?

enter image description here

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@android:drawable/sym_def_app_icon" />


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

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="android.studio@android.com" />

</LinearLayout>

MainActivity.java

NavigationView navigationView = null;
ImageView imgProfile = null;
String URL = "http://something.com/image.jpg";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

navigationView = (NavigationView) findViewById(R.id.nav_view);
    View hView =             navigationView.inflateHeaderView(R.layout.nav_header_main);
imgProfile = (ImageView)hView.findViewById(R.id.imageView);
    Glide.with(getApplicationContext()).load(URL).into(imgProfile);
    navigationView.setNavigationItemSelectedListener(this);
}

1 个答案:

答案 0 :(得分:1)

删除此

employees
来自Xml中的Navigationview的