【问题标题】:Remove unnecessary white space above navigation bar删除导航栏上方不必要的空白
【发布时间】:2021-02-23 15:23:10
【问题描述】:

如何删除导航栏和布局之间不必要的空白(请参阅屏幕截图)?在 android studio 中,没有显示空白,但是在运行应用程序时,这个不需要的空白会自动添加。我在两个真正的安卓设备和模拟器上测试过,结果是一样的。

布局 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical"
android:visibility="visible"
android:weightSum="1"
tools:context="com.test.test.MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:visibility="visible" />

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:background="@color/blueui"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn1"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn2"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn3"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn4"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn5"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/button_background"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/btn6"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="12sp" />
    </LinearLayout>
</HorizontalScrollView>
</LinearLayout>

应用主题

<resources>

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>

</style>
</resources>

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    这是由于 HorizontalScrollView 滚动条的位置,如果您想使用相同的代码但让白色区域消失,您可以将 layout_weight 更改为 0.11,它可能会解决您的问题,但可能会破坏显示按钮图像。

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.11"
        android:background="@color/blueui"
        android:fillViewport="true">
    .....
    </HorizontalScrollView>
    

    【讨论】:

    • 您好,谢谢。是的,但是我尝试通过将 android:layout_marginBottom="-1dp" 添加到 root linearayout 来进行类似的尝试,但是在 android studio 中,布局位于导航栏的顶部,我猜这将导致未来的问题。(不确定每个屏幕都会如何尺寸可用)。逻辑上权重必须等于 1 不是吗? :)
    • 有人说负边距是一种可行的解决方案,而另一些人则说不是。您可以查看这篇文章以获得不同的观点:stackoverflow.com/q/10673503/2335336。只要您的视图在负值中没有“可点击”按钮/对象,我认为这应该不是问题。