gpt4 book ai didi

java - 在Android中设置Snackbar的高度

转载 作者:太空狗 更新时间:2023-10-29 16:28:40 25 4
gpt4 key购买 nike

我有一个 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);

最佳答案

我们将提供多个答案。先说一两句!您可以设置 Snackbar 的高度和宽度,但这很麻烦且耗时。关于 Snackbar 小部件的一个认识是大多数教程不讨论样式。意见是它们应该只是小部件给你的大小而不是我的观点。所以我们注意到文本大小和最大行数起着很大的作用是样式良好的 Snackbar 的大小。所以设计你的 Snackbar 和风格吧好的如何实现困惑建议不要这样做声明这个变量,你会在你的 Activity 中声明任何其他变量

 RelativeLayout rl;

然后当您需要增加 XML 文件中的 RelativeLayout 的大小时,但在这种情况下不是根布局,请使用此代码

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

当您完成增加的尺寸(这可能会混淆根布局中其他对象的尺寸)时,您可能需要将根布局的尺寸设置回原来的尺寸。在这种情况下,根布局设置为布局高度 615dp,我们正在使用 Nexus 7 平板电脑。如果您还没有注意到这一点,这里是 1480 以像素为单位的 MESS 部分,您需要以 dp 为单位。我确信可以进行转换,只是不要问我。所以这里是设置后退的代码行

 rl.getLayoutParams().height = 1230;

现在介绍一种简单的方法来设计和设计两种类型的 Snackbar,一种带有 Action 按钮,另一种没有。首先,你需要一个 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>

现在我们准备在 Activity 中做一些工作,在设置一些高级字符串和颜色后设计和设置样式。请不要被冒犯,我说得很透彻,因为您似乎对编程很陌生。

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

现在对于 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>

在你声明变量的 Activity 中完成内务管理并添加这个

    private CoordinatorLayout myLayout;
Snackbar sb = null;

private CoordinatorLayout noActLayout;
Snackbar sbNoAct = null;

这里有两种类型的 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 ===============");
}
};

享受代码,如果这解决了您的问题,请通过评论告诉我们。

关于java - 在Android中设置Snackbar的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271811/

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