2016-11-12 41 views
1

我试图用导航抽屉来创建应用程序。我需要的是能够通过使用滑动从代码的导航抽屉头部动态更改图像。放上这段代码后,我会像在图片上一样获得导航头两次。如何解决它?Android导航抽屉从URL设置图像

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="[email protected]" /> 

</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

从Navigationview删除此

app:headerLayout="@layout/nav_header_main" 

在XML中