Android - ImageView 空白(仅限 xml)



我的新项目遇到了一个奇怪的问题,我只是无法在我的活动中显示图像。 图像视图显示一个空白方块。

截图

如果我尝试使用黑色背景和透明的图片,即使它应该是彩色的,我也可以看到显示为白色的图片。 但是,如果我尝试使用没有透明度的多色图片,它只会显示一个空白的正方形。

我尝试了模拟器和手机,结果完全相同。

我在没有任何工作解决方案的情况下检查了 Stackoverflow 上的所有相关帖子。

我的源 :

我在res/drawable/test2中添加了一张小图片.png

这是我的(非常基本的(活动代码/activity_main.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test2"
/>
</LinearLayout>

我的活动 : 公共类 MainActivity 扩展 AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

清单:

<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

还有我的构建文件: 应用插件:"com.android.application">

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.test.myappid"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support:design:27.1.1'
compile 'com.evernote:android-job:1.2.4'
compile 'com.android.volley:volley:1.1.0'
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}

感谢您的帮助!

你能尝试使用

android:scaleType:"centerInside">

看看这是否是扩展问题?另外,图像的尺寸是多少?

我会诚实地使用glide来管理图片!从长远来看,以这种方式管理图片会更容易!

https://github.com/bumptech/glide

此外,请确保为缓存优化设置以下代码

@Override public void applyOptions(Context context, GlideBuilder builder) {
builder.setMemoryCache(new MemoryCacheAdapter());
}

好吧,我的坏,我只是在复制/粘贴方面失败了。

我的风格是这样的.xml:

<item name="android:tint">@color/colorLight</item>

删除它解决了这个问题。

最新更新