android splash 公司,Android Splash Screen最佳实践,包含全面屏,刘海屏适配

随着iPhone X的发布,一众Android厂商也开始跟风推出自己的刘海屏,全面屏手机,整个手机行业已经从传统的16:9逐渐往18:9,18.5:9等屏幕比例过渡,其中也给我们开发着带来了一个比较棘手的问题,那就是Splash Screen对全面屏手机的适配问题。市面上通用的方案就是创建适配多种分辨率的drawable文件夹,这种方案有缺点明显,不能完美的适配各种宽高比的屏幕。于是乎就有了下面这种方案就是使用layer-list来进行适配,先看效果:

16:9屏幕

全面屏不带刘海

全面屏带刘海

105885c44e49

105885c44e49

105885c44e49

可以看出,无论是普通的16:9屏幕还是全面屏,刘海屏都可以完美适配。

上代码:

在drawable目录创建一个layer-list,如splash.xml

android:gravity="center|bottom"

android:src="@drawable/splash_logo" />

在style.xml中为SplashActivity创建一个theme

@drawable/splash

true

在values-v21中为SplashActivity创建一个和SplashTheme同名的一个theme

创建这个theme的主要目的是为了适配有navigation bar的手机,不让windowBackground延申到navigationBar的区域内

@drawable/splash

true

false

在AndroidManifest.xml中定义SplashActivity的theme为SplashTheme

android:name=".SplashActivity"

android:theme="@style/SplashTheme">

SplashActivity布局(参考)

需注意的是logo占位图的marginBottom要与上面layer-list中的bottom属性的值保持一致,这就可视觉上和windowBackground中的logo的位置保持完全一致。使得下面的logo在任意一种屏幕下都能保持居中的位置。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/iv_ad"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:contentDescription="广告图"

android:scaleType="centerCrop"

android:src="@drawable/ad_img" />

android:id="@+id/logo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="40dp"

android:layout_marginBottom="40dp"

android:src="@drawable/splash_logo"

android:contentDescription="logo图,占位"/>

刘海屏适配

需解决的问题是让布局延申到刘海(状态栏)区域,方法如下:

针对Android p,在values-v28中为SplashActivity创建一个和SplashTheme同名的一个theme,

@drawable/splash

true

false

shortEdges

SplashActivity的onCreate中

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

Window window = getWindow();

View decorView = window.getDecorView();

decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {

@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)

@Override

public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

WindowInsets defaultInsets = v.onApplyWindowInsets(insets);

return defaultInsets.replaceSystemWindowInsets(

defaultInsets.getSystemWindowInsetLeft(),

0,

defaultInsets.getSystemWindowInsetRight(),

defaultInsets.getSystemWindowInsetBottom());

}

});

ViewCompat.requestApplyInsets(decorView);

//将状态栏设成透明,如不想透明可设置其他颜色

window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));

}

华为手机

在AndroidManifest.xml中的SplashActivity的节点添加如下meta-data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义 Android 应用程序的启动幕,您可以按照以下步骤操作: 1. 创建一个新的 XML 布局文件,用于定义您的启动幕的外观和感觉。例如,您可以添加应用程序的名称、标志、背景图像等。 2. 在 Android 项目的清单文件中指定您的启动幕主题。您可以使用 @style/Theme.AppCompat.NoActionBar 样式,将应用程序的主题设置为无操作栏的主题。 3. 创建一个新的活动,并将其指定为您的应用程序的启动活动。在此活动中,设置您的自定义布局,并在适当的时候关闭启动幕。 以下是一个示例代码: 1. 创建一个名为 splash_screen.xml 的 XML 布局文件: ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/logo" android:layout_centerHorizontal="true" android:text="My App Name" android:textColor="#ffffff" android:textSize="24sp" /> </RelativeLayout> ``` 2. 在 AndroidManifest.xml 中为应用程序指定主题: ```xml <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> <!-- Your activities --> </application> ``` 3. 创建一个名为 SplashScreenActivity 的新活动,并将其指定为您的应用程序的启动活动。在此活动中,设置您的自定义布局,并在适当的时候关闭启动幕。 ```java public class SplashScreenActivity extends AppCompatActivity { private static int SPLASH_SCREEN_TIMEOUT = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); startActivity(intent); finish(); } }, SPLASH_SCREEN_TIMEOUT); } } ``` 在此示例中,我们在 onCreate() 方法中设置自定义布局。我们还使用了一个名为 Handler 的类,在 3 秒后启动我们的主活动 MainActivity,并关闭启动幕。 希望这可以帮助您自定义您的 Android 应用程序的启动幕!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值