gpt4 book ai didi

Android动态移除tabBar(隐藏)

转载 作者:搜寻专家 更新时间:2023-11-01 07:48:32 27 4
gpt4 key购买 nike

在我的项目中,我通过 ViewPager 创建我的 tabBar,如下所示:

MainActivity.java

mViewPager = (ViewPager) findViewById(R.id.content_main_viewpager);

if ( mViewPager != null ) {
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

adapter.addFragment( new LandingSearch(), "" );
adapter.addFragment( new LandingWiki(), "" );
adapter.addFragment( new LandingForum(), "" );

mViewPager.setAdapter(adapter);
mViewPager.setOffscreenPageLimit( 3 );

final TabLayout tabs = (TabLayout) findViewById(R.id.content_main_tabs);

if ( tabs != null ) {
tabs.post(new Runnable() {
@Override
public void run() {
tabs.setupWithViewPager( mViewPager );
tabs.getTabAt(0).setIcon(R.drawable.ic_tab_search);
tabs.getTabAt(1).setIcon(R.drawable.ic_tab_favorite);
tabs.getTabAt(2).setIcon(R.drawable.ic_tab_forum);
}
});

}
}

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="com.mathleaks.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<com.mathleaks.ui.util.MainViewPager
android:id="@+id/content_main_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


</com.mathleaks.ui.util.MainViewPager>

<android.support.design.widget.TabLayout
android:id="@+id/content_main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="center"
android:animateLayoutChanges="true"
android:background="@color/background"
app:background="@color/background"
app:layout_scrollFlags="scroll|enterAlways" >

</android.support.design.widget.TabLayout>

</LinearLayout>

现在,当应用程序中发生特殊事件时,我想在运行时将我的 tabBar 隐藏在我的第二个 tabBar fragment (LandingWiki) 中。到目前为止我还没有成功。

final TabLayout tabs = (TabLayout)findViewById(R.id.content_main_tabs);
tabs.setVisibility(View.GONE);

结果:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setVisibility(int)' on a null object reference

最佳答案

这可能是因为 findViewById 引用了 fragment 而不是主要 Activity 。

您可以执行以下操作:在您的 fragment 中(在 onActivityCreated 事件之后)

final TabLayout tabs = (TabLayout)getActivity().findViewById(R.id.content_main_tabs);
tabs.setVisibility(View.GONE);

关于Android动态移除tabBar(隐藏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com