启动画面的背景不可见

时间:2016-10-01 06:02:45

标签: android xml

我正在尝试制作闪屏。我已将背景图像添加到启动画面的xml中。背景图像的大小仅为382 kb。但它没有显示出来。但在预览中它会向我显示图像。

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_ground"
    tools:context="com.wisaver.app.updated.activities.SplashSecond">

</RelativeLayout>

另一个splash xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_ground_2"
    tools:context=".updated.activities.SplashActivity">


    <!--<ImageView
        android:id="@+id/ic_splash"
        android:layout_gravity="center"
        android:background="@drawable/back_ground_2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />-->
    <ImageView
        android:id="@+id/iv_splash"
        android:layout_gravity="center"
        android:background="@drawable/logo"
        android:layout_width="300sp"
        android:layout_height="140dp" />


</FrameLayout>

清单

<activity
        android:name=".updated.activities.SplashActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".updated.activities.SplashSecond"
        android:screenOrientation="portrait" />

我正在Nexus 5上测试它。我正在使用buildToolsVersion 24.0.0开发应用程序。如何使它工作?这里的一些帮助表示赞赏。

public class SplashActivity extends AppCompatActivity {


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

    Thread th=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                startActivity(new Intent(SplashActivity.this,SplashSecond.class));
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });

    th.start();
}
}

这是另一个

public class SplashSecond extends AppCompatActivity {

//boolean letGo=true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_second);
    Thread th=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(400);
                startActivity(new Intent(SplashSecond.this,ConsumerRetailerActivity.class));
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
  }

3 个答案:

答案 0 :(得分:0)

更改两项活动中的Thread代码,如下所示:

new Thread() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        myhandler.post(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, SplashSecond.class));
                finish();
            }
        });

    }
}.start();

或您也可以尝试以下代码:

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        // This method will be executed once the timer is over
        // Start your app main activity

        startActivity(new Intent(SplashActivity.this, SplashSecond.class));

        // close this activity
        finish();
    }
}, SPLASH_TIME_OUT);

答案 1 :(得分:0)

显示启动画面的最简单方法。

1.在activity_splash.xml

中创建res/layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imgSplash"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"/>
</LinearLayout>

2.在给定代码下面添加SplashActivity.java

public class SplashActivity extends AppCompatActivity implements Runnable {
    private static int SPLASH_DELAY = 2000; //miliseconds
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ImageView imgSplash = (ImageView) findViewById(R.id.imgSplash);

        //set your image
        imgSplash.setImageResource(R.mipmap.ic_launcher);

        Handler handler = new Handler(getMainLooper());
        handler.postDelayed(this,SPLASH_DELAY);
    }

    @Override
    public void run() {
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }
}

3. AndroidManifest.xml

之间<application>...</application>的变化
<activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>

答案 2 :(得分:0)

请使用低分辨率图像而不是高分辨率,因为有时需要很长时间才能加载并意味着屏幕会因定时器而消失