您的位置:首页 > 移动开发 > Android开发

在android中,关于去掉标题栏的问题

2016-03-09 17:39 429 查看
方法一:通过隐藏标题栏在需要隐藏标题栏的Activity中通过添加如下一行代码:
getSupportActionBar().hide();
其添加位置示例如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_extrude);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    /*隐藏标题栏*/
    getSupportActionBar().hide();
}
方法二:通过移除标题栏关于移除标题栏,我参考了网上的很多方法,但对于我来说,不知为何都不管用,但仔细一想,标题栏也是布局文件的一个子控件,所以就去对应的activity的xml文件中去查找标题栏所对应的布局,然后将其从activity的xml文件中删除,这样就达到了我们取出标题栏的目的了。例如:我之前带标题栏的xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"    tools:context="com.example.dai.app2.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" />    <android.support.design.widget.FloatingActionButton        android:id="@+id/fab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom|end"        android:layout_margin="@dimen/fab_margin"        android:src="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>
在上述布局文件中,我们只需要移除AppBarLayout子布局文件即可达到我们取出标题栏的目的;若在我们的java代码中,有关于这个子布局文件的控件的相关设置,则其所涉及的代码也需要一并删除,不然在程序运行时会因为代码找不到所对应的控件而报错其修改之后的布局文件如下:
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"    tools:context="com.example.dai.app2.MainActivity">        <include layout="@layout/content_main" />    <android.support.design.widget.FloatingActionButton        android:id="@+id/fab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom|end"        android:layout_margin="@dimen/fab_margin"        android:src="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: