【问题标题】:Android Studio space between my spinner and page title我的微调器和页面标题之间的 Android Studio 空间
【发布时间】:2025-12-28 04:50:12
【问题描述】:

我最近开始使用 Android Studio,因为我想为我的 Android 设备创建一个应用。
我选择底部导航活动作为模板并添加了一个微调器。我将微调器放在页面标题下的最高位置,但是当我在 Android 10 手机上通过调试启动应用程序时,微调器和页面标题之间有很大的空白。但是预览中没有。
如果重要的话,我有一个小米红米 Note 8T Global。

我的片段代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.myfragment.MyFragment"
    tools:layout_editor_absoluteY="81dp">

    <Spinner
        android:id="@+id/myspinner"
        android:layout_width="391dp"
        android:layout_height="wrap_content"
        android:minHeight="48dp"
        android:spinnerMode="dropdown"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:ignore="SpeakableTextPresentCheck" />


</androidx.constraintlayout.widget.ConstraintLayout>

在 android studio 中预览

应用中的外观

提前致谢

【问题讨论】:

    标签: android android-studio spinner


    【解决方案1】:

    尝试从您的约束布局中删除 tools:layout_editor_absoluteY="81dp"。 绝对布局可让您指定其子项的确切位置(x/y 坐标)。

    【讨论】:

    • 不幸的是没用...也许我的手机尺寸和预览有问题...我无法在Android Studio中选择确切的值,所以我选择了最接近的.我的手机是 6.3 英寸(对角线)1080 x 2340,所以最接近的是 Pixel 5,不是吗?
    • 你确定你没有包含另一个工具栏吗?
    • 尝试分享你的主要活动xml文件可能有问题
    • 天哪,谢谢你让我再次查看主要的activity.xml,有一行:android:paddingTop="?attr/actionBarSize"&gt;我删除了它,中间没有更多的空间,非常感谢!
    【解决方案2】:

    从您的代码中删除以下行以解决问题::

    工具:layout_editor_absoluteY="81dp"

    app:layout_constraintBottom_toBottomOf="parent" 应用程序:layout_constraintVertical_bias="0.0"

    【讨论】:

    • 也没有用...也许我要在这里改变一些东西? ibb.co/tbLYwYL
    【解决方案3】:

    试试这个解决问题::

    <?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"
        xmlns:tools="http://schemas.android.com/tools">
    
        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/myspinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="48dp"
            android:spinnerMode="dropdown"
            android:entries="@array/spinnnerItem"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我不知道为什么,但它并没有改变什么?我还尝试删除约束,所以它会跳到 0,0,但顶部和微调器之间仍然只有一个大白框。
    【解决方案4】:

    它的工作原理大致相同(没有凌乱的预览设置):

    <?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:fitsSystemWindows="true">
    
        <Spinner
            android:id="@+id/myspinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="48dp"
            android:spinnerMode="dropdown"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    虽然实际的区别是,Activity 的主题与 XML 预览不同。一个主题有一个ActionBar - 而另一个没有(一些.NoActionBar 主题)。因此,您可能希望在 AndroidManifest.xml 中应用相同的主题,用于 XML 预览。

    【讨论】:

      【解决方案5】:

      我查看了我的 MainActivity.xml 文件,其中有一行代码:android:paddingTop="?attr/actionBarSize"&gt; 只需将其删除,就不会再出现白条了!

      感谢franvillacis,他让我再次查看我的 MainActivity.xml

      【讨论】: