有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在启动屏幕中显示图像

我是一名安卓开发新手。我正在尝试创建一个启动屏幕。我有一张png图片。 我创建了一个带有文本框的空启动屏幕。我有两个活动——主要活动和第二活动

代码:

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);
    }


}


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();
            }
        });
    }
}

主要活动。xml:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

但是app:srcCompat=“@drawable/geeks”是红色的。意味着它还没有解决。我应该在哪里复制启动屏幕的png图像?我还没有在安卓 studio的任何地方复制过这张图片。如果没有图像视图,启动屏幕可以正常运行,显示hello world


共 (1) 个答案

  1. # 1 楼答案

    尝试编写android:src=“@drawable/geeks”,而不是