如何将Snackbar文本对齐方式更改为居中?下面的代码不起作用
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), intent.getStringExtra(KEY_ERROR_MESSAGE), Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red_EC1C24));
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
snack.show();
答案 0 :(得分:28)
试试这个:
// make snackbar
Snackbar mSnackbar = Snackbar.make(view, R.string.intro_snackbar, Snackbar.LENGTH_LONG);
// get snackbar view
View mView = mSnackbar.getView();
// get textview inside snackbar view
TextView mTextView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
// set text to center
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
// show the snackbar
mSnackbar.show();
答案 1 :(得分:22)
tv.setGravity(Gravity.CENTER_HORIZONTAL);
修改强>
Snackbar
库v23中Support
的外观已更改,因此现在的正确答案是:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
答案 2 :(得分:5)
以下代码为我提供了诀窍:
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
if(tv!=null) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
答案 3 :(得分:2)
上述条件setGravity()
解决方案对我不起作用。
对于防弹居中的文字:
setTextAlignment()
com.google.android.material.R.id.snackbar_text
醇>
请注意 AndroidX 中的支持库更改。如果使用AndroidX,请按android.support.design.R.id.snackbar_text
而不是TextView sbTextView = (TextView) findViewById(com.google.android.material.R.id.snackbar_text);
sbTextView.setGravity(Gravity.CENTER_HORIZONTAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sbTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
查找TextView。
代码:
<Button Command="{Binding EditCommand}"
答案 4 :(得分:0)
使用XML的另一种解决方案:
Theme.MaterialComponents
,并且必须为Snackbar
及其TextView
修改其样式,如下所示:<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="snackbarStyle">@style/MySnackbar</item>
<item name="snackbarTextViewStyle">@style/MySnackbarTextView</item>
</style>
Theme.MaterialComponents
在小吃店中添加边距,并更改其动画。如果要使用AppCompat
样式,请执行以下操作:<style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
...
<item name="android:layout_margin">0dp</item>
<item name="animationMode">slide</item>
</style>
TextView
:<style name="MySnackbarTextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
...
<item name="android:gravity">center_horizontal</item>
<item name="android:textAlignment">center</item>
</style>
答案 5 :(得分:0)
使用“com.google.android.material”而不是“android.support.design.R”,因为它在 androidx 迁移后不再受支持。
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar mSnackbar = Snackbar.make(view, R.string.copyright, Snackbar.LENGTH_SHORT);
View mView = mSnackbar.getView();
TextView mTextView = (TextView) mView.findViewById(com.google.android.material.R.id.snackbar_text);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mSnackbar.show();
}
});
答案 6 :(得分:-1)
请尝试使用以下代码,以便在中心设置快餐栏文字:
Snackbar snackbar = Snackbar.make(main_layout, "This is snack", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setGravity(Gravity.CENTER);
TextView mTextView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
snackbar.show();