Android Studio-Image 显示在设计视图中但不在模拟器中 [英] Android Studio-Image showing in design view but not in emulator

查看:59
本文介绍了Android Studio-Image 显示在设计视图中但不在模拟器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 android 应用程序开发.因此,当我构建一个简单的应用程序以在屏幕上显示两个图像时,我使用了 ImageView.图像显示在 android studio 设计屏幕中,但是当我尝试运行使用 genymotion 模拟器的应用程序,屏幕只是空白.我看不到图像.这是 xml 文件.

 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizo​​ntal_margin"android:paddingRight="@dimen/activity_horizo​​ntal_margin"android:paddingTop="@dimen/activity_vertical_margin"工具:上下文=showmebioapp.studio.com.showmebio.MainActivity"android:background="@android:color/holo_blue_bright"><图像视图android:layout_width="wrap_content"android:layout_height="wrap_content"应用程序:srcCompat="@drawable/vegeta"android:layout_marginTop="32dp"android:id="@+id/imageVegetaId"android:layout_alignParentTop="true"android:layout_alignRight="@+id/imageGokuId"android:layout_alignEnd="@+id/imageGokuId"机器人:layout_marginRight="32dp"android:layout_marginEnd="32dp"/><图像视图android:layout_width="wrap_content"android:layout_height="wrap_content"应用程序:srcCompat="@drawable/悟空"机器人:layout_marginBottom="79dp"android:id="@+id/imageGokuId"android:layout_marginLeft="62dp"android:layout_marginStart="62dp"android:layout_alignParentBottom="true"机器人:layout_alignParentLeft =真"android:layout_alignParentStart="true"/></RelativeLayout>

<块引用>

主要活动.JAVA

public class MainActivity extends Activity 实现 View.OnClickListener {私人 ImageView vegetaImage;私有 ImageView gokuImage;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);vegetaImage=(ImageView)findViewById(R.id.imageVegetaId);gokuImage=(ImageView)findViewById(R.id.imageGokuId);vegetaImage.setOnClickListener(this);gokuImage.setOnClickListener(this);}@覆盖public void onClick(View v) {开关(v.getId()){案例 R.id.imageVegetaId:Intent vegetaIntent=new Intent(MainActivity.this,DetailsActivity.class);vegetaIntent.putExtra("vegetaBio","来自vegeta的你好");开始活动(vegetaIntent);休息;案例 R.id.imageGokuId:Intent gokuIntent=new Intent(MainActivity.this,DetailsActivity.class);gokuIntent.putExtra("悟空生物","悟空你好");开始活动(悟空意图);休息;}}

}

请帮助我找到解决方案.

解决方案

你正在使用 srcCompat 而不是使用 src 就像使用 back goku 而不是 goku

 

因为 srcCompat 属性实际上是在 AppCompat 库中定义的.

重要,您需要为此添加适当的命名空间.

<块引用>

xmlns:app="http://schemas.android.com/apk/res-auto"

重要

<块引用>

您得到的似乎只是一个 lint 错误,可能是忽略.我已经尝试过并且有同样的错误,但它正在工作正确.了解更多信息

快乐编码:)

I am learning android app development.So while I was building a simple app to show two images on the screen,I have used ImageView.The images are displayed in the android studio design screen,but when I am trying to run the app using the genymotion emulator,the screen is just blank.I am not able to see the images.here is the xml file.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="showmebioapp.studio.com.showmebio.MainActivity"
    android:background="@android:color/holo_blue_bright">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/vegeta"
        android:layout_marginTop="32dp"
        android:id="@+id/imageVegetaId"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/imageGokuId"
        android:layout_alignEnd="@+id/imageGokuId"
        android:layout_marginRight="32dp"
        android:layout_marginEnd="32dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/goku"
        android:layout_marginBottom="79dp"
        android:id="@+id/imageGokuId"
        android:layout_marginLeft="62dp"
        android:layout_marginStart="62dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

MAIN ACTIVITY.JAVA

public class MainActivity extends Activity implements View.OnClickListener {

private ImageView vegetaImage;
private ImageView gokuImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vegetaImage=(ImageView)findViewById(R.id.imageVegetaId);
    gokuImage=(ImageView)findViewById(R.id.imageGokuId);
    vegetaImage.setOnClickListener(this);
    gokuImage.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch(v.getId()){

        case R.id.imageVegetaId:
            Intent vegetaIntent=new Intent(MainActivity.this,DetailsActivity.class);
            vegetaIntent.putExtra("vegetaBio","hello from vegeta");
            startActivity(vegetaIntent);
            break;
        case R.id.imageGokuId:
            Intent gokuIntent=new Intent(MainActivity.this,DetailsActivity.class);
            gokuIntent.putExtra("gokuBio","hello from goku");
            startActivity(gokuIntent);
            break;
    }
}

}

Please Help me in finding me the solution.

解决方案

You are using srcCompat instead of using src just like using back goku instead of goku

  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:src="@drawable/vegeta"
    android:layout_marginTop="32dp"
    android:id="@+id/imageVegetaId"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/imageGokuId"
    android:layout_alignEnd="@+id/imageGokuId"
    android:layout_marginRight="32dp"
    android:layout_marginEnd="32dp" />

as it srcCompat attribute is actually defined within AppCompat library.

Important you will need to add appropriate namespace for this.

xmlns:app="http://schemas.android.com/apk/res-auto"

Important

what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.for more info

Happy coding :)

这篇关于Android Studio-Image 显示在设计视图中但不在模拟器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆