gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:24:18 25 4
gpt4 key购买 nike

我需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com