如何实现layer-list双颜色占比闪屏适配多屏幕的响应式UI
解决方案
核心问题原因
- 原始layer-list方案中写死dp高度,不同设备的屏幕高度、像素密度存在差异,无法保证固定比例
- 之前LinearLayout方案权重使用错误:使用垂直方向权重分配高度时,子控件的
android:layout_height必须设为0dp,你写的match_parent会导致权重计算逻辑异常,触发报错 - SplashActivity重写了错误的OnCreate重载,带
PersistableBundle参数的重载需要配置persistableMode属性才会触发,常规页面启动不会调用该方法,同时你没有调用SetContentView加载布局,导致布局不生效
方案1:使用ConstraintLayout实现(推荐,比例控制更精准)
直接使用百分比属性控制高度,不需要计算权重,适配性更强:
<?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" android:background="@android:color/white"> <!-- 上半部分40%高度蓝色区域 --> <View android:id="@+id/top_blue_area" android:layout_width="match_parent" android:layout_height="0dp" android:background="#00549F" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHeight_percent="0.4"/> <!-- 底部logo,距离底部80dp水平居中 --> <ImageView android:layout_width="wrap_content" android:layout_height="80dp" android:src="@drawable/Image_null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginBottom="80dp"/> </androidx.constraintlayout.widget.ConstraintLayout>
方案2:修复原有LinearLayout实现
修改权重相关配置即可正常使用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <!-- 上半部分40%高度蓝色区域 --> <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.4" android:background="#00549F"/> <!-- 下半部分60%高度白色区域 --> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.6" android:background="@android:color/white"> <ImageView android:layout_width="wrap_content" android:layout_height="80dp" android:layout_gravity="center_horizontal|bottom" android:layout_marginBottom="80dp" android:src="@drawable/Image_null"/> </FrameLayout> </LinearLayout>
修复SplashActivity代码
修正OnCreate重载,添加布局加载逻辑:
public class SplashActivity : AppCompatActivity { static readonly string TAG = "X:" + typeof(SplashActivity).Name; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 替换为你实际的布局文件名 SetContentView(Resource.Layout.splash_layout); Log.Debug(TAG, "SplashActivity.OnCreate"); // 闪屏延迟跳转,避免用户看不到闪屏直接跳转,延迟时长可自定义 Task.Delay(1000).ContinueWith(t => { StartActivity(new Intent(this, typeof(MainActivity))); Finish(); }, TaskScheduler.FromCurrentSynchronizationContext()); } }
内容的提问来源于stack exchange,提问作者Priyanka
相关产品推荐
相关产品推荐

