将 .png 图像添加到 DrawerLayout 会出错
Adding .png image to DrawerLayout gives error
我正在尝试以 .png 格式将图像添加到侧面菜单 (DrawerLayout) 的顶部,我将图像导入到 "drawable" 文件夹,现在我调用要显示的图像:
android:src="@drawable/icone"
icone 是图像的名称,但出现以下错误:
FATAL EXCEPTION: main
Process: com.example.bugdroid.menuexe, PID: 5076
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bugdroid.menuexe/com.example.bugdroid.menuexe.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
我想把图片放在这里,在白色方块上:
布局:
<?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="@drawable/icone"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Sintomas"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duarte Andrade "
android:id="@+id/textView" />
</LinearLayout>
问题不是 .png。您的图像不适合可用内存。这意味着内存分配超过了堆限制,或者您的进程需要超过堆限制的内存量。
鉴于您正在使用有限的内存,理想情况下您只想在内存中加载较低分辨率的版本。较低分辨率版本应与显示它的 UI 组件的大小相匹配。具有更高分辨率的图像不会提供任何可见的好处,但仍会占用宝贵的内存并由于额外的动态缩放而导致额外的性能开销。
解决方案
尝试使用 Glide 库。要使用 glide 库,您需要将其添加到您的 grandle 依赖项中:
compile 'com.github.bumptech.glide:glide:3.7.0'
和 compile 'com.android.support:support-v4:19.1.0'
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
最后加载您的图像:
ImageView imageview = (ImageView) findViewById(R.id.imageView);
Glide.with(this).load(R.drawable.icone).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
如果 Glide 不起作用,请尝试 this
希望对你有帮助。
我正在尝试以 .png 格式将图像添加到侧面菜单 (DrawerLayout) 的顶部,我将图像导入到 "drawable" 文件夹,现在我调用要显示的图像:
android:src="@drawable/icone"
icone 是图像的名称,但出现以下错误:
FATAL EXCEPTION: main
Process: com.example.bugdroid.menuexe, PID: 5076
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bugdroid.menuexe/com.example.bugdroid.menuexe.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
我想把图片放在这里,在白色方块上:
布局:
<?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="@drawable/icone"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Sintomas"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duarte Andrade "
android:id="@+id/textView" />
</LinearLayout>
问题不是 .png。您的图像不适合可用内存。这意味着内存分配超过了堆限制,或者您的进程需要超过堆限制的内存量。 鉴于您正在使用有限的内存,理想情况下您只想在内存中加载较低分辨率的版本。较低分辨率版本应与显示它的 UI 组件的大小相匹配。具有更高分辨率的图像不会提供任何可见的好处,但仍会占用宝贵的内存并由于额外的动态缩放而导致额外的性能开销。
解决方案
尝试使用 Glide 库。要使用 glide 库,您需要将其添加到您的 grandle 依赖项中:
compile 'com.github.bumptech.glide:glide:3.7.0'
和 compile 'com.android.support:support-v4:19.1.0'
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
最后加载您的图像:
ImageView imageview = (ImageView) findViewById(R.id.imageView);
Glide.with(this).load(R.drawable.icone).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
如果 Glide 不起作用,请尝试 this 希望对你有帮助。