android - 如果文本太长,为什么 Snackbar 文本会消失?

标签 android material-design material-ui snackbar

我需要在 Android 中使用标准 Snackbar 添加一些用户反馈。

我注意到,如果我的文本太长,那么整个文本字符串就会消失。而不是有一个省略号...

更具体地说;在 Pixel 2 模拟器上,以下字符串消失:

“您处于离线状态。请检查您的网络连接,然后重试。” - 这不见了

但是如果我删除最后一个字符,那么它会显示:

“您处于离线状态。请检查您的连接并重试”- 这显示

更糟糕的是,在屏幕较小的设备上,将显示的文本长度在 Nexus 5 模拟器上的含义会减少,例如:

“您处于离线状态。请检查您的连接并重试”- 现在不显示

但如果我进一步缩短它:

“您处于离线状态。请检查您的连接”- 这显示

这对我来说就像一个错误。

我知道 Snackbar 应该用于非常短的文本,但无论如何它不应该任意删除文本。它至少应该显示一些文本或省略号或多行。

有没有其他人注意到这个或者可以建议这是一个错误还是我遗漏了什么?

代码示例:

Snackbar.make(findViewById(R.id.placeSnackBar),
        "You're offline. Check your connection and try again.",
        Snackbar.LENGTH_INDEFINITE)
        .setAction("X", View.OnClickListener { }).show()

我也试过:

val sb = Snackbar.make(this, message, Snackbar.LENGTH_INDEFINITE)
        .setActionTextColor(Color.parseColor("#666666"))
        .setAction("X", View.OnClickListener { })

sb.view.setBackgroundColor(Color.WHITE)
sb.view.findViewById<TextView>(android.support.design.R.id.snackbar_text)
        //.setTextColor(Color.parseColor("#F44336"))
        .setTextColor(Color.parseColor("#000000"))
sb.view.findViewById<TextView>(android.support.design.R.id.snackbar_text)
        .singleLine = false
sb.view.findViewById<TextView>(android.support.design.R.id.snackbar_text)
        .maxLines = 2
sb.show()

用于在 float 页脚布局上方显示 snackbar 的协调器布局的 XML:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/message_bar_container"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_above="@+id/llFooter" />

        <LinearLayout
            android:id="@+id/llFooter"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/footer_background"
            android:orientation="horizontal"
            android:paddingStart="20dp"
            android:paddingEnd="20dp">

            <TextView
                android:id="@+id/login_forgot_password"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
 ...
 />

            <Button
                android:id="@+id/btn_login"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
...
   />

        </LinearLayout>

    </RelativeLayout>

完整字符串的外观:

enter image description here

如果我将字符串缩短 1 个字符:

enter image description here

最佳答案

通过访问其 TextView 将您的 snackbar 行设置为 2:

val message = "You're offline. Check your connection and try again.";
val sb = Snackbar.make(
    findViewById(R.id.newLinearLayout),
    "You're offline. Check your connection and try again.",
    Snackbar.LENGTH_INDEFINITE)
    .setAction("X") {}
val view = sb.view
val textView = view.findViewById<View>(android.support.design.R.id.snackbar_text) as TextView
textView.maxLines = 2
sb.show()

关于android - 如果文本太长,为什么 Snackbar 文本会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53231647/

相关文章:

android - 修复了新 Material 设计 android 中的选项卡

dart - 将 Material 选择绑定(bind)到我的模型

javascript - ReactJS Material -UI : Accept only positive unsigned integer values in TextField

reactjs - material-ui-next - 动态设置调色板颜色

android - 环境类型的 getExternalFilesDir() 方法未定义?

android - java.lang.IllegalStateException : Could not initialize plugin: MockMaker 错误

带有 CardView 的 Android RecyclerView - 以类似堆栈的方式显示卡片(不是列表)

android - 创建 adb pull bash 命令

android - 给定一个 Android 音乐播放列表名称,如何找到播放列表中的歌曲?

reactjs - 如何在 react-app 中重用自定义 Material-ui 按钮?