android - 查看某个 Fragment 时隐藏 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 文件是标准文件,我仍然不知道如何在用户单击相机选项卡时隐藏应用栏,并在他滑回另一个片段时将其返回。
解决方案
操作栏属于活动,而不是您的片段。但是您可以创建一个没有操作栏的样式并更改片段开头的样式。
基于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);
}
推荐阅读
- wordpress - wordpress 多语言 slug 翻译
- machine-learning - IBM Watson NLC - 使用超过 20,000 个文本示例进行培训?
- django - 根据管理站点中的用户输入批量更新模型字段
- android - Android Studio Top-5 评分表关注
- java - 输入凭据,然后将 JSON 发布到 api java
- c++ - 我尝试创建临时对象的方式是否有任何错误
- statistics - Z3统计意义
- javascript - 反应中心 div
- javascript - 带有nodejs axios的数组文件下载器和香草javascript中的异步等待模式
- excel - 如何在 VBA 中调用函数/返回值?