如何解决查看特定片段后隐藏AppBarLayout
我是android的新手,它试图通过选项卡实现主要活动,每个选项卡都包含一个片段,在其中一个片段上,我希望整个应用栏消失(它原本是相机片段),全屏模式下的特定片段,这是我到目前为止的内容:
文件: MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = findViewById(R.id.fab);
final AppBarLayout bar = findViewById(R.id.appBarLayout2); // the app bar layout that i want gone
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
}
@Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
@Override
public void onTabReselected(TabLayout.Tab tab)
{
onTabSelected(tab);
}
});
如您所见,我正在尝试与ViewPager一起实现选项卡侦听器,我曾尝试在bar.position()上的if语句内部实现:
- ((AppCompatActivity)getActivity())。getSupportActionBar()。hide(); -应用程序崩溃
- getActionBar()。hide()-返回null并崩溃
- getWindow()。setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); -不起作用
文件: main_activity.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".UIActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view_pager">
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@mipmap/logo1" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabIndicatorColor="#ffffff"
app:tabIndicatorHeight="2dp" /><!-- #86B2E8 -->
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="@+id/view_pager"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.constraintlayout.widget.ConstraintLayout>
cameraFragment.java和xml文件是标准文件,我仍然不知道当用户单击“ camera”选项卡时如何隐藏应用程序栏,并在用户再次滑动回另一个Fragment时将其返回。
解决方法
操作栏属于活动,而不是您的片段。但是您可以创建没有操作栏的样式,并在片段的开头更改样式。
基于https://stackoverflow.com/a/15496425/8244139,您应该使用以下代码:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),R.style.yourCustomTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater,not default inflater
return localInflater.inflate(R.layout.yourLayout,container,false);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。