自定义小吃栏无法正常工作 [英] Custom Snackbar doesn't work properly
问题描述
我正试图显示一个自定义的Snackbar,如以下示例所示:
I'm trying to show a custom Snackbar as in this example:
这是我创建快餐栏的自定义代码:
This is my custom code to create a 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();
}
我的目标是在基本活动中有一个方法可以从任何其他活动中调用.
My goal is to have a method in my base activity to call from any other activity.
但是我的问题是当我显示Snackbar时,它显示在键盘下:
But my problem is when I show the Snackbar, that it is shown under the keyboard:
此外,它不会显示布局的所有视图:
In addition it does not show all views of layout:
<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"/>
如果有人有任何建议,我会很高兴听到.
If anyone has any suggestions I will be happy to hear it.
谢谢.
推荐答案
首先,您不需要以编程方式设置背景色,因为您已经在xml文件中进行了设置,因此请删除这些行
First of all, you don't need to set background colour programmatically, as you are already doing that in xml files, so remove these lines
ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));
第二,尝试将活动的Coordinator Layout
作为参数传递,而不是Linear/Relative Layout
,即在活动中将协调器布局添加为父布局并传递它.
Secondly, try with passing Coordinator Layout
of the activity as the argument, instead of Linear/Relative Layout
, i.e. add coordinator layout as the parent layout in the activity and pass it.
在xml视图中检查协调器布局的布局边界.它不应扩展活动范围.
Check the layout bounds of the coordinator layout in the xml view. It should not extend the bounds of activity.
这篇关于自定义小吃栏无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!