我有一个 Snackbar
,如下所示:
但是,如果 AutoCompleteTextView
的下拉列表太长,下拉列表会阻塞 Snackbar
。
如您在上图中所见,Snackbar
实际上正在显示。然而,它的可见性被长长的下拉菜单挡住了。从上图可以看出
我尝试使用以下 Snackbar 代码
。添加 bringToFront()
帮助不大。
private void showSnackbar(String message) {
Snackbar snackbar
= Snackbar.make(getActivity().findViewById(R.id.content), message, Snackbar.LENGTH_LONG);
snackbar.getView().bringToFront();
snackbar.show();
}
R.id.content
是一个 CoordinatorLayout
:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/content"
android:background="?attr/MyActivityBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?attr/headerShadow" />
有没有什么好的方法可以避免 Snackbar
被 AutoCompleteTextView
的下拉菜单覆盖?
最佳答案
对于这种情况,我可能有解决方案。当然,有一些假设,但也许解决方案适合您。
这里的关键是将 AutoCompleteTextView
放入 CoordinatorLayout
并向其添加自定义 CoordinatorLayout.Behavior
。
为您的类(class)创建适当的
行为
:public class AutoCompleteTextViewBehaviour extends CoordinatorLayout.Behavior<AutoCompleteTextView> { public AutoCompleteTextViewBehaviour(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, AutoCompleteTextView child, View dependency) { return dependency instanceof Snackbar.SnackbarLayout; } }
覆盖一个方法
layoutDependsOn
:@Override public boolean layoutDependsOn(CoordinatorLayout parent, AutoCompleteTextView child, View dependency) { return dependency instanceof Snackbar.SnackbarLayout; }
获取对
AutoCompleteTextView
弹出 View 的引用:不幸的是,我还没有找到一个简单的解决方案。但是可以通过反射来完成。
@Nullable private View getPopupList(AutoCompleteTextView child) { try { Field popupField; Class clazz; if (child instanceof AppCompatAutoCompleteTextView) { clazz = child.getClass().getSuperclass(); } else { clazz = child.getClass(); } popupField = clazz.getDeclaredField("mPopup"); popupField.setAccessible(true); ListPopupWindow popup = (ListPopupWindow) popupField.get(child); Field popupListViewField = popup.getClass().getDeclaredField("mDropDownList"); popupListViewField.setAccessible(true); return (View) popupListViewField.get(popup); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
覆盖
onDependentViewChanged
方法:@Override public boolean onDependentViewChanged(CoordinatorLayout parent, final AutoCompleteTextView child, View dependency) { if (popupList == null) { popupList = getPopupList(child); if (popupList == null) { return super.onDependentViewChanged(parent, child, dependency); } } int dropdownBottom = child.getBottom() + child.getDropDownVerticalOffset() + popupList.getHeight(); int snackBarTop = dependency.getTop(); int difference = dropdownBottom - snackBarTop; if (difference > 0) { child.setDropDownHeight(popupList.getHeight() - difference); return true; } else { child.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT); } return super.onDependentViewChanged(parent, child, dependency); }
将行为应用于
.xml
中的AutocompleteTextView
:app:layout_behavior="com.example.package.AutoCompleteTextViewBehaviour"/>
当然这是一个非常基本的解决方案,例如不设置列表高度的动画,但我认为这是一个好的开始。这是完整的 gist .
关于android - 将 Snackbar 放在最高的 z 顺序以避免被 AutoCompleteTextView 下拉列表阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44293412/