Android优化——UI优化(四) 使用stytle

本文介绍了一种通过修改Android应用程序的主题样式来避免Activity启动时出现短暂黑屏现象的方法。通过直接在主题中设置背景,可以有效防止用户看到不美观的默认黑色背景,提升用户体验。

使用style替换背景,防止Activity黑色背景闪过


1.原来的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/img_bg"
              android:orientation="horizontal">

    <Button
        android:layout_weight="1"
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"/>

    <Button
        android:layout_weight="1"
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:text="New Button1"
        android:id="@+id/button3"/>


</LinearLayout>

 

这里写图片描述

这里我们用了android:background="@drawable/img_bg"来设置了背景图片,但是当我们启动activity是有时候会出现一个黑色的背景,然后才出现我们设置的背景,给用户感觉我们的程序运行的很慢

2.解决办法

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:background">@drawable/img_bg</item>
    </style>
</resources>

我们可以给主题设置一个背景,这样就不会给用户感觉我们的应用卡住了

3.知其然知其所以然

1.因为程序的主题是在程序启动的时候加载
2.Layout中设置的背景实在Activity启动之后才加载
所以会让用户看到一个黑色背景闪过的过程。

转载于:https://www.cnblogs.com/ldq2016/p/5226593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值