You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android实现50%分屏双色启动屏(横竖屏适配)

Got it, let's get that splash screen sorted so it stays perfectly split 50/50 top and bottom, no matter if the screen's portrait or landscape! Your current fixed-height layer-list works for one specific screen size, but we need a responsive approach. Here are two solid solutions:

Instead of a drawable, create a layout file that uses ConstraintLayout to split the screen evenly. This adapts automatically to any screen size and orientation.

Create res/layout/activity_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Top yellow half -->
    <View
        android:id="@+id/top_section"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/yellow"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/center_guideline"/>

    <!-- Horizontal guideline at 50% screen height -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/center_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5"/>

    <!-- Bottom red half -->
    <View
        android:id="@+id/bottom_section"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/red"
        app:layout_constraintTop_toBottomOf="@id/center_guideline"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

How it works:

  • The horizontal guideline is locked to 50% of the screen height.
  • The top and bottom views have layout_height="0dp", which lets ConstraintLayout stretch them to fill the space between the guideline and the screen edges.
  • Rotate the screen, and the layout automatically adjusts to keep the 50/50 split.
Solution 2: Custom Drawable (If You Need a Drawable Background)

If you must use a drawable (e.g., setting it as a window background for a no-layout splash activity), create a custom drawable that calculates the split dynamically.

First, make the custom drawable class in your project:

class SplitScreenDrawable(
    private val topColor: Int,
    private val bottomColor: Int
) : Drawable() {

    override fun draw(canvas: Canvas) {
        val halfHeight = bounds.height() / 2f
        
        // Paint top half yellow
        val topPaint = Paint().apply {
            color = topColor
            style = Paint.Style.FILL
        }
        canvas.drawRect(0f, 0f, bounds.width().toFloat(), halfHeight, topPaint)
        
        // Paint bottom half red
        val bottomPaint = Paint().apply {
            color = bottomColor
            style = Paint.Style.FILL
        }
        canvas.drawRect(0f, halfHeight, bounds.width().toFloat(), bounds.height().toFloat(), bottomPaint)
    }

    override fun setAlpha(alpha: Int) {}
    override fun setColorFilter(colorFilter: ColorFilter?) {}
    override fun getOpacity(): Int = PixelFormat.OPAQUE
}

Then set it as your window background in the splash activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // No need for setContentView if you're just using a background drawable
    val yellow = ContextCompat.getColor(this, R.color.yellow)
    val red = ContextCompat.getColor(this, R.color.red)
    window.setBackgroundDrawable(SplitScreenDrawable(yellow, red))
}

How it works:

  • The drawable calculates half the screen height every time it's drawn, so it always adapts to the current screen size and orientation.
  • It directly draws two solid color rectangles that fill exactly half the screen each.

Why your original code didn't work: Fixed 440dp heights only work for one specific screen size. When you rotate or use a different device, the split won't stay 50/50.

内容的提问来源于stack exchange,提问作者Blarzek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:47:46