AppCompat Snackbar在平板电脑上没有居中显示

8

我正在尝试使用appcompat显示一个snackbar,向用户展示一条消息。在手机上它完美地运行,但是在平板电脑上出现了

portrait

landscape

我用来生成snackbar的代码是:

Snackbar.make(mHomeContainer, R.string.rate_snackbar, Snackbar.LENGTH_LONG)
        .setAction("Rate", ...)
        .show();

希望能够提供有关如何使这个小吃店居中的指导,将不胜感激。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/main"
        android:elevation="8dp"/>

    <include layout="@layout/toolbar" />

    <RelativeLayout
        android:id="@+id/home_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true" />

        <ListView
            android:id="@+id/home_search_list"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#88000000"/>

    </RelativeLayout>
</LinearLayout>

<include layout="@layout/navigation_list" />

</android.support.v4.widget.DrawerLayout>

1
根据此链接,Snacker在平板电脑/桌面上不应该是全宽。 - wwang
谢谢,我已经更新了它,改为居中而不是全宽。 - Tim Mutton
mHomeContainer是什么?mHomeContainer是否填满整个屏幕的宽度? - ianhanniballake
它是一个全宽度的RelativeLayout。 - Tim Mutton
发布您的平板电脑布局。 - Gabriele Mariotti
@GabrieleMariotti 添加了完整的布局。 - Tim Mutton
2个回答

7

要实现这个目标,你需要使用CoordinatorLayout。这里有一篇文章介绍了如何做到这一点。此外,您可能需要提供不同于平板电脑的布局文件,以便在手机上保留全宽度。如果您同时使用FAB、应用栏和Snackbar,则还需要使用此布局组件。


如果在某个片段中有嵌套的片段 - 一个在左边,一个在右边。并且在单击左侧的某个项目时,我想在窗口底部中心显示一个 snackbar,该怎么办?我必须在父片段的底部添加 coordinator layout,并在单击某个项目时将调用传递给父片段,以显示 snack bar?虽然不是很有用。 - krossovochkin
这似乎会违背通用的 Material Design 原则。也许消息应该在 fragment 中,或者根据你想要实现的内容,使用 Toast 更合适?我没有尝试过使用多个 CoordinatorLayouts,但也许这对你来说是可能的? - AkiAki007

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接