如果我扩展Activity,ImageView将保持为空,但如果扩展AppCompatActivity,则按预期工作

时间:2017-12-08 17:43:39

标签: android xml android-imageview

我不确定为什么我无法使用ImageView在我的应用中显示图片。有任何想法吗?请询问您是否需要更多信息。谢谢!

activity_main.xml我有:

<ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/armas_barcode" />

也许内容无关紧要。在我的MainActivity public class MainActivity extends Activity {中,图片未打印,也没有记录错误。如果我将其更改为public class MainActivity extends AppCompatActivity {,则会按预期在屏幕上打印图像。图像是矢量图像。

我有其他矢量图片,我在设置/偏好设置中使用android:icon,它们似乎工作正常。

我的build.gradle如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.zebget.pricemap"
        minSdkVersion 25
        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 {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:design:27.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

1 个答案:

答案 0 :(得分:1)

如果布局文件包含不起作用的行,因为它们需要例如一个不同的API级别,然后它们被简单地忽略,代码编译和应用程序工作。或多或少。

如果您的MainActivity扩展Activity,则布局中的以下行将不适用。

  

app:srcCompat="@drawable/armas_barcode"

这是因为app命名空间(xmlns:app =“http://schemas.android.com/apk/res-auto”)仅适用于AppCompatActivity

但您也可以像这样设置ImageView的可绘制资源:

android:src="@drawable/armas_barcode"
相关问题