简体   繁体   English

在 Android 中设置 Snackbar 的高度

[英]Set height of Snackbar in Android

I have a Snackbar in need to set its height or set height to wrap content.我有一个 Snackbar 需要设置它的高度或设置高度来包装内容。 Is there any way?有什么办法吗?

Snackbar snack = Snackbar.make(findViewById(R.id.activity_container), "Message", Snackbar.LENGTH_SHORT);

View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
view.setBackgroundColor(Color.RED);
tv.setTextColor(Color.WHITE);
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER_HORIZONTAL);

We are going to provide multiple answers.我们将提供多种答案。 First a statement or two!首先声明一两个! You can set the height and width of a Snackbar but it is messy and time consuming period.您可以设置 Snackbar 的高度和宽度,但这是一个混乱且耗时的时期。 One realization about a Snackbar widget is most tutorials do not talk about styling.关于 Snackbar 小部件的一个认识是大多数教程不讨论样式。 The opinion is they should be just the size that the widget gives you NOT MY VIEW.意见是它们应该只是小部件给你的大小而不是我的观点。 So we have noticed that the text size and number of max lines plays a BIG roll is the size of a well styled Snackbar.所以我们注意到文本大小和最大行数起到了很大的作用,是一个风格良好的 Snackbar 的大小。 So design your Snackbar and style away OK how to implement the mess Suggestion DO NOT DO THIS declare this variable where you would declare any other variable in your Activity所以设计你的 Snackbar 和 style 好的如何实现混乱建议不要这样做声明这个变量,你会在你的活动中声明任何其他变量

 RelativeLayout rl;

Then when you need to increase the size of your RelativeLayout that is in your XML file but is not the root Layout in this case use this code然后,当您需要增加 XML 文件中的 RelativeLayout 的大小但在这种情况下不是根布局时,请使用此代码

    rl = (RelativeLayout) findViewById(R.id.svRL);
    rl.getLayoutParams().height = 1480;

When you get done with this increased size which can mess with the size of other object in the root Layout you might want to set the size of the root Layout back to what it was.当您完成这个增加的大小时,它可能会干扰根布局中其他对象的大小,您可能希望将根布局的大小设置回原来的大小。 In this case the root Layout was set to layout height 615dp we are working with a Nexus 7 Tablet.在这种情况下,根布局设置为布局高度 615dp,我们正在使用 Nexus 7 平板电脑。 If you have not noticed this yet here is the MESS part that 1480 is in units of pixels and you need it in dp.如果您还没有注意到这一点,这里是 MESS 部分,即 1480 以像素为单位,您需要以 dp 为单位。 I am sure the conversion can be made just do not ask me.我确定可以进行转换,只是不要问我。 So here is the set back line of code所以这是回退的代码行

 rl.getLayoutParams().height = 1230;

Now for a easy way to design and style two types of Snackbar's one with an Action button and one with out.现在有一种简单的方法来设计和样式化两种类型的 Snackbar,一种带有操作按钮,另一种没有按钮。 First you need a CoordinatorLayout in what ever Activity corresponding XML file that looks like this Note it has an id首先你需要一个 CoordinatorLayout 在任何 Activity 对应的 XML 文件中,看起来像这样 注意它有一个 id

        <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coorSB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >
        <!-- android.support.design.widget.SnackBar -->

        <!--stuff you want inside the coordinator ... -->
    </android.support.design.widget.CoordinatorLayout>

Now we are ready to do some work in the Activity to design and style after a little advanced string and color set up.现在我们准备在 Activity 中做一些工作来设计和设置一些高级字符串和颜色后的样式。 Please do not be offended I am being very thorough because you seem to be very new to programming.请不要生气我说得非常彻底,因为您似乎对编程很陌生。

    <string name="snackbar_text">I Am a NEW SnackBAR TEXT</string>
<string name="snackbar_action">EXIT</string>
<string name="second_text">Second Text</string>
<string name="csb_text">I am the Custom Guy</string>
<string name="csb_action">EXIT</string>
<string name="the_text">Password must have one Numeric Value\n"
"One Upper &amp; Lower Case Letters\n"
"One Special Character $ @ ! % * ? &amp;\n"
"NO Spaces in the PASSWORD"</string>

Now for the Rainbow many ways to manage Color this is my mine.现在对于 Rainbow 来说,管理颜色的方法有很多,这是我的。

<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303f9f</color>
<color name="colorAccent">#FF4081</color>
<color name="color_Black">#000000</color>
<color name="color_White">#FFFFFF</color>
<color name="color_darkGray">#606060</color>
<color name="color_lightGray">#C0C0C0</color>
<color name="color_super_lightGray">#E0E0E0</color>
<color name="color_Red">#FF0000</color>
<color name="color_Yellow">#FFFF66</color>
<color name="color_deepBlue">#0000ff</color>
<color name="color_lightBlue">#3333FF</color>
<color name="color_Purple">#9C27B0</color>
<color name="color_Transparent">@android:color/transparent</color>

Done with house keeping in your Activity where you declare variables add this在您声明变量的活动中完成内务管理,添加此

    private CoordinatorLayout myLayout;
Snackbar sb = null;

private CoordinatorLayout noActLayout;
Snackbar sbNoAct = null;

There here is the implementation of both types of Snackbars这里有两种类型的 Snackbars 的实现

    public void makeNoAct(View view){
        // this is declared on a Button android:onClick="makeNoAct"
    noActLayout = (CoordinatorLayout)findViewById(R.id.coorSB);

    sbNoAct = Snackbar.make(noActLayout,R.string.the_text,1);// any interger will make it happy
            sbNoAct.setDuration(3000);// 3 sec               // OR Snackbar.LENGTH_LONG
                                                             // matters NOT you are setting duration
    View sbView = sbNoAct.getView();
    sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_Black));
    TextView textViewNoAct = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    //set text color
    textViewNoAct.setTextColor(ContextCompat.getColor(this,R.color.color_Yellow));
    textViewNoAct.setMaxLines(10);
    textViewNoAct.setTextSize(24);
    //increase max lines of text in snackbar. default is 2.
    sbNoAct.show();

    int height = sbView.getHeight();
    etNewData.setText(String.valueOf(height));

}

public void makeCOOR(View view) {
    // this is declared on a Button android:onClick="makeCOOR"
    // We were to Lazy to write an OnClickListener
    myLayout = (CoordinatorLayout) findViewById(R.id.coorSB);

    sb = Snackbar.make(myLayout, R.string.csb_text, Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.csb_action, myOnClickListener)
            .setActionTextColor(ContextCompat.getColor(context, R.color.color_Red));

    View sbView = sb.getView();
    sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_White));
    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    //set text color
    textView.setTextColor(ContextCompat.getColor(this,R.color.color_deepBlue));
    textView.setTextSize(30);
    //increase max lines of text in snackbar. default is 2.
    textView.setMaxLines(10);
    // NOTE new View
    TextView textAction = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_action);
    //set Action text color
    textAction.setTextColor(ContextCompat.getColor(this,R.color.color_Red));
    textAction.setTextSize(30);
            sb.show();
    }

    View.OnClickListener myOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // OR use and Intent to go somewhere have a nice trip
            sb.dismiss();
            System.out.println("========= I WAS DISMISSED ===============");
        }
    };

Enjoy the code and let us know with a comment if this solves your issue.享受代码,如果这解决了您的问题,请通过评论告诉我们。

final String CR= System.getProperty("line.separator") ;
String snackMsg= "First line" + CR;
   snackMsg+="Second line." +CR;
   snackMsg+="... more lines." +CR;

final Snackbar snack = Snackbar.make(findViewById(android.R.id.content),  snackMsg, Snackbar.LENGTH_INDEFINITE);

snack.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Respond to the click, such as by undoing the modification that caused
                        // this message to be displayed
                    }
                });

View view = snack.getView();

TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);

// tv.setBackgroundColor(Color.RED);
tv.setLines(12);

FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
//params.height=2000;
params.bottomMargin=10;
view.setLayoutParams(params);

snack.show();

This is very simple to change the height or width of Snackbar .更改Snackbar的高度或宽度非常简单。 Just we need to write 2 , 3 line of code to do this.我们只需要编写 2 、 3 行代码来做到这一点。 Check the below code snippet .检查下面的代码片段。

Snackbar snackbar =   Snackbar.make(view, "Your message", Snackbar.LENGTH_LONG);
        snackbar.setAction("Ok", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //your click action.
                    }
                });

        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)snackbar.getView();
        layout.setMinimumHeight(50);//your custom height.
        snackbar.show();

You may also need to set the size for the inner container of a snackbar in order for the text to be aligned/centered vertically.您可能还需要设置小吃店内部容器的大小,以便文本垂直对齐/居中。 Here is a solution (Kotlin):这是一个解决方案(Kotlin):

Snackbar.make( containerView, msg, duration ).also {
            // outer container
            it.view.minimumHeight = minHeightPx

            // inner container
            ( it.view as? Snackbar.SnackbarLayout )?.getChildAt( 0 )?.let { innerView ->
                innerView.minimumHeight = minHeightPx
            }
        }.show()
Snackbar snack = Snackbar.make(findViewById(R.id.activity_container), "Message", Snackbar.LENGTH_SHORT);

View view = snack.getView();

TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.height = 80;
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setLayoutParams(params);
snack.show();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM