You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android新手求助:启动页PNG图片应放置在哪个目录?

解决Android启动页图片无法解析的问题

嗨,作为Android开发新手,这个资源找不到的问题其实非常普遍,别着急,我来一步步帮你解决!

问题原因

你在布局里写了app:srcCompat="@drawable/geeks",但Android Studio的资源目录里还没有这张名为geeks.png的图片,所以IDE无法解析这个资源引用,才会显示红色报错。

正确的图片放置目录

你需要把PNG图片放到项目的**app/src/main/res/drawable**目录下,具体操作步骤:

  • 切换到Android Studio的「Project」视图(默认可能是Android视图,点击左上角的下拉菜单切换)
  • 找到app/src/main/res文件夹,如果里面没有drawable目录:
    1. 右键点击res文件夹 → 选择「New」→「Android Resource Directory」
    2. 在弹出的窗口里,「Resource type」选择drawable,点击「OK」创建目录
  • 把你的PNG图片复制到drawable目录里,注意图片文件名必须是小写字母,不能包含空格、中文或特殊字符(比如你的代码里用的是geeks,所以图片要命名为geeks.png)

完成以上步骤后,回到activity_main.xml里,app:srcCompat="@drawable/geeks"的红色提示就会自动消失了。

额外说明

如果你的图片有不同分辨率的版本(比如适配不同屏幕密度的设备),可以把对应分辨率的图片放到drawable-mdpi、drawable-hdpi、drawable-xhdpi等对应密度的目录里,系统会根据用户设备自动选择最合适的图片加载。但作为新手,先把图片放到通用的drawable目录完全能满足需求。

你的代码参考(已确认逻辑没问题)

MainActivity代码

public class MainActivity extends AppCompatActivity {
    private static int SPLASH_SCREEN_TIME_OUT=2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i=new Intent(MainActivity.this, SecondActivity.class);
                //Intent is used to switch from one activity to another.
                startActivity(i);
                //invoke the SecondActivity.
                finish();
                //the current activity will get finished.
            }
        }, SPLASH_SCREEN_TIME_OUT);
    }
}

SecondActivity代码

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

activity_main.xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="200dp"
        app:srcCompat="@drawable/geeks"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.447" />

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者peepal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:34:30