2017-05-30 335 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 ==============="); 
     } 
    }; 

享受的實施代碼,並讓我們知道如果這可以解決您的問題。