2025-11-20 14:47:38 by wst
android开发好的页面在适配不同机型的时候发现,国内的手机基本都没问题。但是在pixel6上就遮盖了状态栏。索性直接加个上边距,把状态栏的空间留出来,下面描述修改步骤;
1.首先建立一个xml文件,把上边距的高度设定一下;这里新建的文件是:app/src/main/res/values/dimens.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 状态栏高度(默认24dp,Pixel 6 适配刚好,可后续微调) -->
<dimen name="toolbar_top_padding">24dp</dimen>
<dimen name="layout_top">16dp</dimen>
</resources>
2. 在布局文件(app/src/main/res/layout/activity_main.xml)中设置下元素的上边距,注意androidx.appcompat.widget.Toolbar中android:paddingTop="@dimen/toolbar_top_padding"的设置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background_light"
tools:context=".MainActivity">
<!-- 标题栏 -->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:paddingTop="@dimen/toolbar_top_padding"
android:paddingEnd="8dp"
app:titleTextColor="@color/text_light"
app:title="@string/app_title"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<ImageButton
android:id="@+id/settingsButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_marginEnd="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/add_contact"
android:src="@android:drawable/ic_input_add"
app:tint="@color/white_alpha_70" />
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
3. 在子页面(app/src/main/res/layout/activity_contact_settings.xml)中也可以手动设置,如下(注意:android:layout_marginTop="@dimen/layout_top"):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@color/background_light"
tools:context=".ContactSettingsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_top"
android:orientation="vertical"
android:padding="24dp">
</LinearLayout>
</ScrollView>
在采用这个方法之前,也想着能不能动态计算,测试发现:计算一堆,而且效果还没有这个好,所以就放弃了。
在探索Android开发的路上,我们一起前行!
--- 爱学习,爱分享的小鲤鱼。