我在平板电脑(API 22)上设置Snackbar
时出现问题,在手机(API 23和22)上设置正常(从边缘到边缘),即使在水平方向也是如此。
结果是如下Snackbar:
FloatingActionButton(支持库)也不会移动(当它在手机上时)。
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/snackParent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="@dimen/margin_fab"
android:layout_marginRight="@dimen/margin_fab"
app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>
在MainActivity中使用
private void showMessage(String msg) {
if(snackParent != null) {
snackbar = Snackbar.make(snackParent, msg, Snackbar.LENGTH_SHORT);
snackbar.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
}
}
我只有平板电脑尺寸(工厂边距)的其他资源文件(w820dp),手机和平板电脑之间的样式文件夹相同。我也试过使Android Studio缓存无效。
我使用com.android.support:design:23.0.1
targetSdkVersion = 23和compileSdkVersion = 23,buildToolsVersion = 23.0.1。
答案 0 :(得分:5)
我认为这是平板电脑上零食吧的默认行为。检查this。
答案 1 :(得分:1)
你可以用零食吧做任何你想做的事。我们在平板电脑上遇到了同样的问题,我们解决了这个问题(例如,下面的代码是在顶部定位零食店,并为其提供父母的全宽度)
View view = snackbar.getView();
// Position snackbar at top
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
view.setLayoutParams(params);
答案 2 :(得分:0)
ViewGroup.LayoutParams layoutParams =快餐栏View.getLayoutParams();
if (layoutParams instanceof FrameLayout.LayoutParams) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutParams;
params.gravity = Gravity.TOP;
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
snackbarView.setLayoutParams(params);
} else if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layoutParams;
params.gravity = Gravity.TOP;
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
snackbarView.setLayoutParams(params);
} else {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutParams;
params.gravity = Gravity.TOP;
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
snackbarView.setLayoutParams(params);
}