我使用圖層串列創建了 windowBackground,其中我使用android:gravity="center". 然后我顯示相同的影像,但使用具有 ImageView 的相對布局,并使用android:layout_centerInParent="true". 但是相對布局中的影像略高。如何使它們居中,使它們處于同一位置?
splash_background 檔案中作為 windowBackground 的圖層串列:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/appBackground" />
<item>
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@raw/splash_animated" />
</item>
</layer-list>
具有此相對布局的活動(使用以下樣式):
<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:orientation="vertical"
tools:context=".LaunchAnimatedActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@raw/splash_animated" />
</RelativeLayout>
風格:
<style name="AppTheme.FullscreenSplash" parent="AppTheme.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/splash_background</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
uj5u.com熱心網友回復:
我使用這段代碼將背景影像與活動影像對齊:
val statusBar = resources.getIdentifier("status_bar_height", "dimen", "android")
if (statusBar != 0) {
//getting statusbar height
val statusBarHeight = resources.getDimensionPixelSize(statusBar)
val imageView = findViewById<ImageView>(R.id.splash_image)
// using FrameLayout instead of RelativeLayout
imageView.updateLayoutParams<FrameLayout.LayoutParams>
{
//orientation aware
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
leftMargin = statusBarHeight
} else {
topMargin = statusBarHeight
}
}
}
uj5u.com熱心網友回復:
框架布局是解決方案。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/splash"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:adjustViewBounds="true"
android:scaleType="fitXY"
</FrameLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522126.html
標籤:安卓安卓布局定心
上一篇:為什么這種觀點忽略了它的約束?
