2

这是我的 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: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" android:orientation="vertical"
    android:gravity="bottom">

    <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />

    <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:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="android.studio@android.com" android:id="@+id/textView" />

</LinearLayout>

这是我的 oncreate 方法,我正在使用导航视图

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

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(MainActivity.this));
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        ImageView imageView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.imageView);
        ImageLoader.getInstance().displayImage("https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", imageView);
    }

我试过这个,但图像根本没有改变?什么都没有发生,我已经包含了universalimageloader.jar,并添加了所有依赖项。可能是什么问题?

4

2 回答 2

2

我假设您在链接中使用NavigationViewfrom : Android blogspotAndroid Design Support Library

<android.support.v4.widget.DrawerLayout
    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="match_parent"
    android:fitsSystemWindows="true">

<!-- your content layout -->

<android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

drawer_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="100dp"
          android:gravity="bottom"
          android:orientation="vertical"
          android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
    android:id="@+id/ivHeader"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

这就是我使用以下方法ImageView在标题中加载图像的方式UniversalImageLoader

initImageLoader(this);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
ImageView imageView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.ivHeader);
ImageLoader.getInstance().displayImage("YourImageUrl", imageView);

请注意,从版本compile 'com.android.support:design:23.1.1'中,HeaderView可以使用navigationView.getHeaderView(0). 希望能帮助到你。

于 2015-11-25T10:15:59.650 回答
0

Kingfisher 的答案是正确的,但是图像缓存不好,我尝试使用 Ion 库,它的功能也是如此......并且更好地缓存图像。

OnCreate
{
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        imageView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.imageView);
loadWebImage(imageView);
}

     private void loadWebImage(final ImageView imageView) {
            final String URL = "http://d22r54gnmuhwmk.cloudfront.net/app-assets/global/default_user_icon-7a95ec473c1c41f6d020d32c0504a0ef.jpg";
            Ion.with(this).load(URL).asBitmap().setCallback(new FutureCallback<Bitmap>() {
                @Override
                public void onCompleted(Exception e, Bitmap result) {
                    if (e == null) {
                        imageView.setImageBitmap(result);
                    } else {
                        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();

                        Toast.makeText(getApplicationContext(),"not found",Toast.LENGTH_LONG).show();
                        imageView.setImageResource(R.mipmap.ic_launcher);
                    }
                }
            });
        }
于 2015-11-25T11:36:01.590 回答