我有一个主要活动ConstraintLayout,它需要在顶部AppbarLayout下方可见的区域中显示ViewPager LiveData,该区域容纳ViewPager的滚动TabLayout项名称。我的困难在于,ViewPager片段文本的顶部被AppbarLayout工具栏和选项卡遮盖了。我试图将ViewPager数据限制为显示在AppbarLayout区域下方。
我可以在AS随附的TabLayout活动模板中执行此操作,并且当TabLayout本身时,它可以按需工作。不幸的是,我的活动还迎合了DrawerLayout NavigationView菜单,并且Drawer和ViewPager交替响应滑动的唯一方法是将ViewPager嵌套在放置在其NavigationView之前的DrawerLayout中。因此,我的ViewPager已经在XML规范底部的ActivityLayout的DrawerLayout中关闭:
<ConstraintLayout
...
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
app:layout_constraintTop_toBottomOf="@+id/cal_appbar">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:visibility="visible">
</androidx.viewpager.widget.ViewPager>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_refresh"
app:menu="@menu/activity_calib_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
</ConstraintLayout>
在它和我显示省略号的根标记之间,ConstraintLayout具有一个AppbarLayout,其中包含工具栏和TabLayout,后跟一个RelativeLayout(用于其他片段)。该问题不涉及RelativeLayout,而是AppbarLayout与ViewPager的显示重叠]
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/cal_appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" android:fitsSystemWindows="true" tools:context=".Calibsense" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/cal_toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="@color/colorPrimary" android:visibility="visible" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minHeight="?actionBarSize" android:padding="@dimen/appbar_padding_top" android:text="@string/intro_name" android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"/> </androidx.appcompat.widget.Toolbar> <com.google.android.material.tabs.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:visibility="visible" app:layout_constraintTop_toBottomOf="@id/cal_appbar"> </com.google.android.material.tabs.TabLayout> </com.google.android.material.appbar.AppBarLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/overlap"> ... </RelativeLayout> ... </ConstraintLayout>
[ConstraintLayout坚持约束同级,所以我上面的约束指定包含受AppbarLayout约束的视图DrawerLayout,而不是最终目标:针对TabLayout的ViewPager。
是否存在可用于指定片段(ViewPager)显示在AppbarLayout-TabLayout下面的约束试探法?
作为一种可能的解决方法,有没有一种方法可以使用AppbarLayout的高度为ViewPager指定顶部填充,以将其内容推入可见区域?
我也考虑过尝试使用CollapsingToolbarLayout,但是设法显示了整个AppPageLayout都没有的整个ViewPager。
附录:
在Activity对setGuide()的onCreate()方法调用期间,进一步查看了上述内容的编程调用,我在下面公开了AppbarLayout参数,调试器向我显示了与XML设置相同的值:height -1(match_parent)和width -2(wrap_content),并且在使用setGuide()末尾的子视图进行扩展后,该值保持不变。测量的高度始终显示零值。我不知道这些价值观的重要性。我希望看到几何值,例如非零px / dp数量。谁能阐明一些想法?为0的measuredHeight是否还意味着ViewPager锚定在px(0,0),如果是,则如何将锚定y值更改为AppbarLayout的可见底部以下?但是我需要首先以编程方式提取该值。如何实现?
DrawerLayout drwLyt;
SectionsPagerAdapter sectionsPagerAdapter;
ViewPager viewPager;
AppBarLayout aBL;
CollapsingToolbarLayout cTBL;
TabLayout tabs;
TextView title;
ConstraintLayout mainGuideView;
Calibsense guideParent;
void setGuide(Calibsense guideModule){
guideParent = guideModule;
// AppbarLayout
aBL = guideParent.findViewById(R.id.cal_appbar);
ViewGroup.LayoutParams aBLParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
aBL.setLayoutParams(aBLParams);
aBL.setExpanded(true);
// measure AppbarLayout using params copy of internal value
ViewGroup.LayoutParams appBarLayoutParams = aBL.getLayoutParams();
int aBLHeight = appBarLayoutParams.height;
int aBLmHeight = aBL.getMeasuredHeight();
drwLyt = findViewById(R.id.drawer_layout);
ViewGroup.LayoutParams drawerLayoutParams = drwLyt.getLayoutParams();
int drwLytHeight = drawerLayoutParams.height;
// setting-up of viewPager
sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
viewPager = findViewById(R.id.view_pager);
// this could be redundant,
// somewhere it was suggested a new LayoutParams() is required
ViewGroup.LayoutParams viewPagerParams = viewPager.getLayoutParams();
viewPager.setAdapter(sectionsPagerAdapter);
int viewPagerHeight = viewPager.getHeight();
View spacerView = findViewById(R.id.pager_spacer);
int spacerHeight = spacerView.getHeight();
spacerView.setMinimumHeight(aBLHeight);
// Start set-up of tabs
TabLayout tabs = findViewById(R.id.tabs);
// complete set-up of tabs by insertion of viewPager
tabs.setupWithViewPager(viewPager);
int tabsHeight = tabs.getHeight();
relL.setVisibility(View.INVISIBLE);
// measure AppbarLayout using params copy of internal value
appBarLayoutParams = aBL.getLayoutParams();
aBLHeight = appBarLayoutParams.height;
aBLmHeight = aBL.getMeasuredHeight();
}
我有一个主要活动ConstraintLayout,它需要在顶部AppbarLayout下方可见的区域中显示ViewPager LiveData,该区域容纳ViewPager的滚动TabLayout项名称。我的...
似乎是规范:android:fitsSystemWindows =“ true”似乎替代了其他约束并将可见项锚定在(0,0),但是对我而言,该替代项的范围和范围尚不清楚。现在避免它。