简体   繁体   English

带有多行的浮动操作按钮 Snackbar

[英]Floating Action Button Snackbar with multi line

When I click Action button Snack bar shows only two line of text and third line not showing当我单击操作按钮时,小吃栏只显示两行文本,第三行不显示

this is XML content这是XML内容

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_menu_help" />

And I use this code in my activity我在我的活动中使用此代码

......
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Hello word1" + "\n" + "Hello word2" + "\n" + "Hello word3", Snackbar.LENGTH_LONG).setAction("Action", null).show();
        }
    });
 .......

Actually Android Snackbar have a TextView to show your text.实际上 Android Snackbar有一个TextView来显示您的文本。 So by setting Multi line in TextView you can achieve your target.因此,通过在TextView设置多行,您可以实现您的目标。 Try this code, it worked for me.试试这个代码,它对我有用。

 Snackbar snackbar = Snackbar.make(Yourview, "Your Text", Snackbar.LENGTH_LONG);
 View sbView = snackbar.getView();

 TextView textView =  sbView.findViewById(android.support.design.R.id.snackbar_text);

 // For multi-line text, limit max line count.
 textView.setMaxLines(3);

 snackbar.show();

This code works for me这段代码对我有用

public void onCreate(Bundle savedInstanceState) {
    //....
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showSnack(view, "My three line message", 5000);
        }
    });
     //....
}


public void showSnack(View view, String text, int duration) {
    snackbar = Snackbar.make(view, text, duration);
    View sbView = snackbar.getView();
    TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextSize(16);
    textView.setMaxLines(3);
    snackbar.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 浮动动作按钮+小吃栏的容器 - Container for Floating Action Button + Snackbar Snackbar在按下动作按钮时隐藏了浮动动作 - Snackbar hides the Floating Action on pressing the action button on it Snackbar顶部的浮动动作按钮。 怎么样? - Floating Action Button on top of Snackbar. How? 浮动动作按钮在第二个Snackbar下消失 - Floating Action Button diasappears under SECOND Snackbar 如果在 CoordinatorLayout 中设置了 anchorView,则浮动操作按钮不会随 Snackbar 一起浮动 - Floating Action Button Not Floating With Snackbar If anchorView set within CoordinatorLayout 浮动操作按钮不会停留在SnackBar的顶部 - Floating Action Button won't stay on top of SnackBar Snackbar 通过片段导航重叠浮动操作按钮 - Snackbar Overlapping Floating Action Button Through Fragment Navigation Android底部工作表,带有带有Snackbar问题的浮动操作按钮 - Android Bottom Sheet with Floating Action Button with Snackbar Issue 上下移动浮动操作按钮以避免被快餐栏阻挡 - Moving Floating Action Button up and down to avoid getting blocked by a snackbar 当snackbar animation 开始时浮动动作按钮上下跳跃 - Floating Action Button jumps up and down when snackbar animation begins
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM