ViewPager 中的部分片段在屏幕底部被截断(Android)
Part of fragment inside ViewPager getting cut off at bottom of screen(Android)
我已经为我的视图定义了一个基本的 Coordinator 布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
我的视图寻呼机中有多个选项卡。我正在发布我的一个简单片段:
<ListView
android:id="@+id/transferList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_alignParentRight="true"
android:src="@drawable/ic_add_white" />
发生的事情是 FAB 超出了屏幕。只是不是 FAB,而是其他带有 cardviews/listviews 的片段在屏幕底部被截断了。我四处寻找解决方案,并提出了一个 hacky 解决方案:从工具栏 中删除 属性 app:layout_scrollFlags="scroll|enterAlways"。
我不明白是什么导致了这种情况发生,以及如何通过删除上述内容解决问题。它是支持库中的一些错误吗?有没有更好的方法来解决这个问题?我遇到的另一个解决方案来自 - 将 FAB 按钮直接放在 activity 中的协调器布局下,并使其仅在您需要的片段中可见。对我来说似乎不是一个好的解决方案。另外,我的片段中的其他视图也被切断了。
这是因为您正在使用 CoordinatorLayout
和 ListView
。您可以将实现更改为 RecyclerView
以实现正确的滚动。
检查我的回答 。这可能对你有帮助。
我已通过将 工具栏 的 app:layout_scrollFlags="scroll|enterAlways"
属性更改为仅 app:layout_scrollFlags="enterAlways"
.
解决了这个问题
我的布局与您的类似:activity 顶部有一个工具栏和选项卡。而碎片的要求是有自己独立的FAB
我无法直接在 xml 中处理它,所以我就这样解决了:
Activity布局
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginBottom="20dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabContentStart="72dp"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
片段布局:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/scroll"
layout="@layout/content_activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:src="@android:drawable/ic_input_add"
app:borderWidth="0dp"
app:fabSize="mini"
app:useCompatPadding="true" />
还有一些代码可以让 FAB 保持原样。
创建视图时的片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_tabs2, container, false);
final FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
final TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
final AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int maxAbsOffset = appBarLayout.getMeasuredHeight() - tabLayout.getMeasuredHeight();
fab.setTranslationY(-maxAbsOffset - verticalOffset);
}
});
return rootView;
}
从工具栏 layout_scrollFlags 属性中删除滚动。你应该有这样的东西:
app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed"
这个问题有2个解决方案:
- 您可以从
app:layout_scrollFlags
中删除工具栏的 scroll
(例如 app:layout_scrollFlags="enterAlways"
)或删除整个 app:layout_scrollFlags
- 如果需要
scroll
属性,主布局content/fragment必须使用RecyclerView
或NestedScrollView
.
https://code.luasoftware.com/tutorials/android/bottom-of-fragment-in-viewpager-out-of-screen/
我已经为我的视图定义了一个基本的 Coordinator 布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
我的视图寻呼机中有多个选项卡。我正在发布我的一个简单片段:
<ListView
android:id="@+id/transferList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_alignParentRight="true"
android:src="@drawable/ic_add_white" />
发生的事情是 FAB 超出了屏幕。只是不是 FAB,而是其他带有 cardviews/listviews 的片段在屏幕底部被截断了。我四处寻找解决方案,并提出了一个 hacky 解决方案:从工具栏 中删除 属性 app:layout_scrollFlags="scroll|enterAlways"。
我不明白是什么导致了这种情况发生,以及如何通过删除上述内容解决问题。它是支持库中的一些错误吗?有没有更好的方法来解决这个问题?我遇到的另一个解决方案来自
这是因为您正在使用 CoordinatorLayout
和 ListView
。您可以将实现更改为 RecyclerView
以实现正确的滚动。
检查我的回答
我已通过将 工具栏 的 app:layout_scrollFlags="scroll|enterAlways"
属性更改为仅 app:layout_scrollFlags="enterAlways"
.
我的布局与您的类似:activity 顶部有一个工具栏和选项卡。而碎片的要求是有自己独立的FAB
我无法直接在 xml 中处理它,所以我就这样解决了:
Activity布局
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginBottom="20dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabContentStart="72dp"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
片段布局:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/scroll"
layout="@layout/content_activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:src="@android:drawable/ic_input_add"
app:borderWidth="0dp"
app:fabSize="mini"
app:useCompatPadding="true" />
还有一些代码可以让 FAB 保持原样。 创建视图时的片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_tabs2, container, false);
final FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
final TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
final AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int maxAbsOffset = appBarLayout.getMeasuredHeight() - tabLayout.getMeasuredHeight();
fab.setTranslationY(-maxAbsOffset - verticalOffset);
}
});
return rootView;
}
从工具栏 layout_scrollFlags 属性中删除滚动。你应该有这样的东西:
app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed"
这个问题有2个解决方案:
- 您可以从
app:layout_scrollFlags
中删除工具栏的scroll
(例如app:layout_scrollFlags="enterAlways"
)或删除整个app:layout_scrollFlags
- 如果需要
scroll
属性,主布局content/fragment必须使用RecyclerView
或NestedScrollView
.
https://code.luasoftware.com/tutorials/android/bottom-of-fragment-in-viewpager-out-of-screen/