代码之家  ›  专栏  ›  技术社区  ›  ligi

SnackBar和fitsSystemWindow

  •  21
  • ligi  · 技术社区  · 9 年前

    我有一个应用程序使用fitsSystemWindows可以在导航栏和状态栏后面绘制背景,不幸的是SnackBar似乎忽略了容器中的fitsSystemWindows=true。我将问题归结为这个最小的应用程序:

    problem

    样式:

    <resources>
    
        <style name="AppTheme" parent="Theme.AppCompat.Light">
    
            <item name="android:windowBackground">@color/accent_material_dark</item>
            <item name="android:fitsSystemWindows">false</item>
    
            <item name="android:windowTranslucentStatus">true</item>
            <item name="android:windowTranslucentNavigation">true</item>
        </style>
    
    </resources>
    

    布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
    
                    android:fitsSystemWindows="true"
                    tools:context=".MainActivity">
    
        <Button
    
                android:id="@+id/button"
                android:text="@string/hello_world"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
    </RelativeLayout>
    

    活动:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    Snackbar.make(v,"This Snackbar should fitSystemWindows",Snackbar.LENGTH_INDEFINITE).show();
                }
            });
        }
    
    }
    

    有人知道解决方法吗?

    我发布了一个最小的应用程序,在这里显示了问题: https://github.com/ligi/SnackBarFitsSystemWindowProblem

    3 回复  |  直到 9 年前
        1
  •  17
  •   ianhanniballake    9 年前

    Snackbar 将始终寻找 CoordinatorLayout 锚定自身:当您没有时,它使用完整内容视图(在您的情况下,包括状态栏下的区域)-添加 协调员布局 fitsSystemWindows=true 应该这样做。

        2
  •  -1
  •   Akshay Patel    8 年前

    您也可以使用以下行代替任何 协调员布局 在里面 小吃店声明 具有 findViewById(android.support.design_navigation_view) .

        3
  •  -3
  •   Sepehr Azizi    8 年前

    正确答案是加 android:paddingbottom="50dp" 到CoordinatorLayout 虽然我不确定所有导航栏的大小是否都是50dp