2017-05-30 328 views
1

我有一个Snackbar需要设置其高度或设置高度来包装内容。有什么办法吗?在Android中设置Snackbar的高度

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); 
+0

您可以通过扩展它来创建自定义的SnackBar吗? – Eenvincible

+0

我不知道该怎么做 – user8076850

+0

你能帮我吗 – user8076850

回答

2

我们将提供多个答案。首先是一个或两个声明!您可以设置Snackbar的高度和宽度,但这是一个混乱且耗时的时期。 有关Snackbar小部件的一个实现是大多数教程不讨论样式。意见是它们应该只是小部件给你的大小而不是我的视图。所以我们已经注意到,文字大小和最大线条数量都是BIG卷是一个风格良好的小吃店的大小。所以,设计自己的小吃吧和风格走 确定如何实现的烂摊子建议不要做这个声明这个变量就是当你需要增加你的RelativeLayout的大小,你将宣布任何其他变量在活动

RelativeLayout rl; 

然后这是在你的XML文件,但不能将根布局在这种情况下,使用此代码

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

,当你用它可以乱用其他物体的根布局的大小,你可能想这增加的大小进行将根布局的大小设置回原来的大小。在这种情况下,根布局设置为布局高度615dp,我们正在使用Nexus 7平板电脑。如果你还没有注意到这一点,那么这里是1480像素单位的MESS部分,你需要它在dp中。我相信可以做出转换就是不要问我。因此,这里是代码

rl.getLayoutParams().height = 1230; 

设定的后卫线现在对于一个简单的方法来设计和风格两种小吃吧的一个带有操作按钮和一个跟了出去。首先,你需要相应的,看起来像这样的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> 

现在我们准备做的活动有一定的工作有点高级字符串和颜色后,设计理念和风格在什么都活动CoordinatorLayout建立。请不要被冒犯我是非常彻底的,因为你似乎对编程很陌生。

<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> 

现在的彩虹许多方式来管理颜色这是我的。

<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> 

在你的活动,你声明变量添加此

private CoordinatorLayout myLayout; 
Snackbar sb = null; 

private CoordinatorLayout noActLayout; 
Snackbar sbNoAct = null; 

还有这里看家做的是两种类型的小吃店

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 ==============="); 
     } 
    }; 

享受的实施代码,并让我们知道如果这可以解决您的问题。