【发布时间】:2019-03-03 01:47:26
【问题描述】:
上下文
第 1 步: 目前我有一个活动,它使用android:windowBackground 设置初始背景,同时等待活动加载。这会加载一个应该居中对齐的位图。
第 2 步: 加载活动后,它会执行简单的 setContentView 设置活动的背景,现在将替换 step1 中的 android:windowBackground。这会加载一个应该居中对齐的imageView。
问题是它们都不是中心对齐的,其中一个或另一个未对齐它们存在某种偏移。可能状态栏向下推一个?我不知道。有什么想法为什么它们不一致?
我想让它们都居中对齐。我试过使用fitSystemWindows="true",但没有成功。
当我将 android:layout_marginTop="12dp" 添加到活动 (activity_start_onboarding.xml) 布局 imageView 时,两者都可以很好地对齐,但它并不能针对所有密度对齐,其他密度也不会对齐。
也许有一种方法可以动态计算此偏移量以对齐所有密度?
比较图片
左(灰色)- Step1(窗口背景):
右(蓝色)- Step2(活动布局):
代码
AndroidManifest.xml
<activity android:name=".view.onboarding.ActivityStartOnboarding"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/ColdstartSplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
styles.xml
<style name="ColdstartSplashTheme" parent="SuperbalistTheme.Dark">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
@drawable/splash_background
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/grey_mid_dark" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_delivery_free_raster" />
</item>
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
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue_light_darker"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/splash_background" />
</FrameLayout>
</layout>
【问题讨论】:
-
为什么在
activity_start_onboarding.xml中使用splash_background而在splash_background中使用ic_delivery_free_raster? -
您好,仅供参考。以便主题/样式使用与活动相同的布局。
@drawable/splash_background
标签: android splash-screen