我正在尝试显示一个自定义Snackbar,如以下示例所示:
这是我创建Snackbar的自定义代码:
protected void showSnackBar(View view) {
Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);
LayoutInflater inflater = LayoutInflater.from(this);
View snackView = inflater.inflate(R.layout.custom_snackbar, null);
layout.addView(snackView, 0);
ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));
snackbar.show();
}
我的目标是在基本活动中有一个方法可以从任何其他活动中调用。
但是我的问题是当我显示Snackbar时,它显示在键盘下:
此外,它不会显示布局的所有视图:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativelayout_sync_schedule_runs"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/green_wasabi"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal|bottom">
<ProgressBar
android:id="@+id/progressbar_infinite"
android:layout_width="30dp"
android:layout_height="30dp"
style="@android:style/Widget.Material.ProgressBar.Small"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/textview_sync_runs"
android:layout_toStartOf="@+id/textview_sync_runs" />
<com.runator.ui.widget.text.CustomTextView
android:id="@+id/textview_sync_runs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/import_runs"
android:textColor="@color/black_midnight"
style="@style/texts.medium_medium_big"
app:font_type="bold"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
如果有人有任何建议,我将很高兴听到。
提前致谢。
首先,您不需要以编程方式设置背景色,因为您已经在xml文件中进行了设置,因此请删除这些行
ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));
其次,尝试通过传递Coordinator Layout
活动作为参数,而不是Linear/Relative Layout
,即在活动中添加协调器布局作为父布局并传递它。
在xml视图中检查协调器布局的布局边界。它不应扩展活动范围。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句