启动片段时隐藏ActionBar

Posted

技术标签:

【中文标题】启动片段时隐藏ActionBar【英文标题】:ActionBar hidden when starting fragment 【发布时间】:2016-04-06 17:01:56 【问题描述】:

我有一个活动可以正确显示我的 ActionBar 及其项目,但是当我尝试向该活动添加片段时,该片段启动时 ActionBar 消失,或者如果我尝试包含 ActionBar,它会显示该栏但没有标题或菜单选项。

除非另有说明,否则我想要实现的是一个活动,所有片段都使用相同的 ActionBar。

MainActivity.Java

public class MainActivity extends AppCompatActivity implements     NavigationView.OnNavigationItemSelectedListener 
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final HomeFragment homeFragment = new HomeFragment();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    if (savedInstanceState == null) 
        getFragmentManager()
                .beginTransaction()
                .add(R.id.main_container, homeFragment)
                .addToBackStack(null)
                .commit();
    


@Override
public void onBackPressed() 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) 
        drawer.closeDrawer(GravityCompat.START);
     else 
        super.onBackPressed();
    


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in androidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) 
        return true;
     else if (id == R.id.search_mag_icon)
        // Open intent here
        Intent i = new Intent(getApplicationContext(), SearchActivity.class);
        startActivity(i);
    

    return super.onOptionsItemSelected(item);

HomeFragment.java

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_
        android:layout_>

        <!-- Book icon and text -->
        <ImageView
            android:id="@+id/bookIcon"
            android:layout_
            android:layout_
            android:layout_marginLeft="50dip"
            android:layout_marginTop="110dip"
            android:src="@drawable/book" />

        <TextView
            android:id="@+id/bookLabel"
            android:layout_
            android:layout_
            android:layout_below="@+id/bookIcon"
            android:layout_marginLeft="20dip"
            android:text="@string/bookLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />


        <!-- Barcode icon and text -->
        <ImageView
            android:id="@+id/barcodeIcon"
            android:layout_
            android:layout_
            android:layout_marginLeft="260dip"
            android:layout_marginTop="120dip"
            android:src="@drawable/barcode" />

        <TextView
            android:id="@+id/barcodeLabel"
            android:layout_
            android:layout_
            android:layout_below="@+id/barcodeIcon"
            android:layout_marginLeft="260dip"
            android:paddingTop="5dip"
            android:text="@string/barcodeLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />


        <!-- Message icon and text -->
        <ImageView
            android:id="@+id/messageIcon"
            android:layout_
            android:layout_
            android:layout_marginLeft="50dip"
            android:layout_marginTop="350dip"
            android:src="@drawable/email" />

        <TextView
            android:id="@+id/messageLabel"
            android:layout_
            android:layout_
            android:layout_below="@+id/messageIcon"
            android:layout_marginLeft="55dip"
            android:text="@string/messageLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />

        <!-- List icon and text -->
        <ImageView
            android:id="@+id/listIcon"
            android:layout_
            android:layout_
            android:layout_marginLeft="260dip"
            android:layout_marginTop="350dip"
            android:src="@drawable/list" />

        <TextView
            android:id="@+id/listLabel"
            android:layout_
            android:layout_
            android:layout_below="@+id/listIcon"
            android:layout_marginLeft="273dip"
            android:paddingTop="5dip"
            android:text="@string/listLabel"
            android:textColor="@color/colorAlt"
            android:textStyle="bold" />

    </RelativeLayout>
</FrameLayout>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:background="#FFF"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical" >

        <include
            android:layout_
            android:layout_
            layout="@layout/app_bar_main" />

        <FrameLayout
            android:id="@+id/main_container"
            android:layout_
            android:layout_ >
        </FrameLayout>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimaryDark"
        app:itemTextColor="#FFF"
        app:itemIconTint="#FFF"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_
android:layout_
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/app_bar_main"
android:layout_
android:layout_ />

<!-- Book icon and text -->
<ImageView
    android:id="@+id/bookIcon"
    android:src="@drawable/book"
    android:layout_
    android:layout_
    android:layout_marginLeft="50dip"
    android:layout_marginTop = "110dip"
    />

<TextView
    android:id="@+id/bookLabel"
    android:layout_
    android:layout_
    android:text="@string/bookLabel"
    android:textColor="@color/colorAlt"
    android:textStyle="bold"
    android:layout_marginLeft="20dip"
    android:layout_below="@+id/bookIcon" />


<!-- Barcode icon and text -->
<ImageView
    android:id="@+id/barcodeIcon"
    android:src="@drawable/barcode"
    android:layout_
    android:layout_
    android:layout_marginLeft="260dip"
    android:layout_marginTop = "120dip"
    />

<TextView
    android:id="@+id/barcodeLabel"
    android:layout_
    android:layout_
    android:text="@string/barcodeLabel"
    android:textColor="@color/colorAlt"
    android:textStyle="bold"
    android:layout_below="@+id/barcodeIcon"
    android:paddingTop="5dip"
    android:layout_marginLeft="260dip"/>


<!-- Message icon and text -->
<ImageView
    android:id="@+id/messageIcon"
    android:src="@drawable/email"
    android:layout_
    android:layout_
    android:layout_marginLeft="50dip"
    android:layout_marginTop = "350dip"
    />

<TextView
    android:id="@+id/messageLabel"
    android:layout_
    android:layout_
    android:text="@string/messageLabel"
    android:textColor="@color/colorAlt"
    android:textStyle="bold"
    android:layout_marginLeft="55dip"
    android:layout_below="@+id/messageIcon" />

<!-- List icon and text -->
<ImageView
    android:id="@+id/listIcon"
    android:src="@drawable/list"
    android:layout_
    android:layout_
    android:layout_marginLeft="260dip"
    android:layout_marginTop = "350dip"
    />

<TextView
    android:id="@+id/listLabel"
    android:layout_
    android:layout_
    android:text="@string/listLabel"
    android:textColor="@color/colorAlt"
    android:textStyle="bold"
    android:layout_below="@+id/listIcon"
    android:paddingTop="5dip"
    android:layout_marginLeft="273dip"/>

</RelativeLayout>

提前感谢您的帮助。

【问题讨论】:

也显示你的xml代码。 【参考方案1】:

修改布局

ToolBar 放入LinearLayout 并将Fragment 更改为FrameLayout

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <include
        android:layout_
        android:layout_
        layout="@layout/app_bar_main" />

    <FrameLayout
        android:id="@+id/main_container"
        android:layout_
        android:layout_ >
    </FrameLayout>
</LinearLayout>

Activity拨打Fragment

FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment));
                    fragmentTransaction.commit();

【讨论】:

感谢您的回复,我这样做了,但现在活动启动时没有在屏幕上启动/放置片段,布局的另一个问题? 还是不行=/我把导入的库改成import android.support.v4.app.Fragment;解决了兼容性错误,但是fragment仍然不显示。 fragment_home 布局??。不应为空 我已经更新了原始帖子,您发布的代码 sn-p 是要进入 activity_main 或 fragment_home 吗?对不起,如果我误解了。 我已将您的答案标记为正确并在此处重新提交了一个新问题:***.com/questions/34571336/…

以上是关于启动片段时隐藏ActionBar的主要内容,如果未能解决你的问题,请参考以下文章

覆盖片段中的后退按钮

从片段导航到另一个片段时隐藏键盘

隐藏/显示不同片段的操作栏选项菜单项

在Android Studio片段之间切换时地图片段不隐藏

隐藏系统自带的标题栏

当我尝试在片段中显示或隐藏浮动操作按钮时出错