【问题标题】:AppCompat Snackbar not centered on tabletAppCompat Snackbar 未以平板电脑为中心
【发布时间】:2015-09-05 12:23:01
【问题描述】:

我正在尝试显示一个快餐栏(通过 appcompat)来向用户显示一条消息。它在手机上运行良好,但在平板电脑上我得到了

我用来生成快餐栏的代码是

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>

【问题讨论】:

  • 根据this,Snacker 在平板电脑/桌面上不应该是全宽的。
  • 谢谢,我已经将它更新为居中而不是全宽
  • 什么是mHomeContainermHomeContainer 会填满屏幕的整个宽度吗?
  • 它是一个全宽的RelativeLayout
  • 发布您的平板电脑布局

标签: android material-design android-appcompat


【解决方案1】:

您需要使用 CoordinatorLayout 来实现这一点。这是一个article,描述了如何做到这一点。此外,您现在可能需要提供要用于非平板电脑与平板电脑的布局文件,以便您可以保留手机的全宽。如果你使用FAB、app bar和Snackbar的组合,这也是你需要使用的布局组件。

【讨论】:

  • 如果我在某个片段中嵌套了片段 - 一个在左侧,一个在右侧。单击左侧的某些项目时,我想在窗口的底部中心显示一个快餐栏?我必须在底部的父片段中添加协调器布局,然后单击某些项目我必须传递给父片段调用,应该显示小吃店?不过不是很有用
  • 这看起来你会脱离一般的材料设计原则。也许消息应该在片段中,或者这是 Toast 更适合的东西,这取决于您要完成的工作?我没有尝试使用多个 CoordinatorLayouts,但也许这对你来说是可能的?
【解决方案2】:

在 Design Support Library v22.2.0 中,Snackbar 的默认重力只是 Gravity.BOTTOM。

在 v22.2.1 中已修复此问题:https://code.google.com/p/android/issues/detail?id=176383

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 2019-04-18
    • 2018-04-27
    • 2011-12-14
    • 1970-01-01
    • 2016-03-15
    • 2011-10-02
    • 1970-01-01
    相关资源
    最近更新 更多