android:windowbackground 图文,Center Android WindowBackground with Activity ImageView

Context

Step1: Currently I have one activity which uses android:windowBackground to set the initial background while we wait for the activity to load. This loads a bitmap which should be center aligned.

Step2: Once the activity is loaded it does a simple setContentView to set the background for the activity which will now replace the android:windowBackground from step1. This loads an imageView which should be center aligned.

The problem is they are not both aligned center, there is some kind of offset on one or the other misaligning them. Maby the statusbar pushing down the one? I am not sure. Any ideas why they are not aligned the same?

I would like to have them both center aligned. I have tried using fitSystemWindows="true" with no luck.

When I add android:layout_marginTop="12dp" to the activity (activity_start_onboarding.xml) layouts imageView both align fine but it does not align for all densities, other densities are misaligned.

Maby there is a way to dynamically calculate this offset to align then for all densities?

Compare Images

Left (Grey) - Step1 (windowBackground):

Right (Blue) - Step2 (activity layout):

dedef86c5f59633c9000a3bada110e82.png

The Code

AndroidManifest.xml

android:launchMode="singleTask"

android:screenOrientation="portrait"

android:theme="@style/ColdstartSplashTheme">

styles.xml

@drawable/splash_background

@drawable/splash_background

android:gravity="center"

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

ActivityStartOnboarding.java

public class ActivityStartOnboarding extends AppCompatActivity {

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_start_onboarding);

}

}

activity_start_onboarding.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/blue_light_darker"

android:fitsSystemWindows="true">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

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

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

android:fitsSystemWindows="true"

That is your problem. Your FrameLayout start to inflate under statusbar, which height ~25dp, therefore AppCompatImageView a bit higher.

First solution:

Remove android:fitsSystemWindows="true" from activity_start_onboarding.xml FrameLayout.

Second solution:

Add true to your ColdstartSplashTheme. The background starts drawing under status bar and bitmap will be higher.

# Answer 2

Use background instead of windowBackground

@drawable/splash_background

# Answer 3

For me the accepted solution didn' work. But I found this and it worked like a charm.

Its also a more flexible solution because it doesn't depend on API 21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值