移除工具栏标题

3

主活动:

package gc.mp3onlinemusic;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.home,
            R.drawable.download
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }
}

我有OneFragment和TwoFragment类用于选项卡。我想要移除标题栏。我尝试了以下方法:
requestWindowFeature(Window.FEATURE_NO_TITLE);

它无法正常工作

我也尝试过

android:theme="@android:style/Theme.NoTitleBar"

在清单文件中,出现了停止错误。

导致原因:java.lang.IllegalStateException: 您需要使用 Theme.AppCompat 主题(或派生主题)与此活动一起使用。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gc.mp3onlinemusic">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MyMaterialTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Styles.xml

<resources>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

最后,如何在安卓中移除标题栏?

2
你在哪里添加了 "requestWindowFeature(Window.FEATURE_NO_TITLE);" ? - Sanjeet
在 setConventView 代码行之前。 - serkan stack
7个回答

2
有两种处理方法:
  • 编程方式:
getSupportActionBar().setDisplayShowTitleEnabled(false)
  • 从XML(样式)
<item name="windowActionBar">false</item>

我认为你只需要隐藏工具栏中的标题,而无需执行类似于getSupportActionBar().setTitle(...)这样的操作。


1
你可以尝试这个:

你可以尝试这个:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setContentInsetsAbsolute(0, 0); // remove toolbar margin from left/right

setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");

1
我无法按照建议的方法去掉标题,所以我通过在导航图文件中删除android:label属性来解决问题。
例如:
<fragment
    android:id="@+id/navigation_bookmarks"
    android:name="some_label" <!-- REMOVE THIS LINE -->
    android:label="@string/title_bookmarks"
/>

0
如果您想隐藏整个操作栏,请在您的活动类中使用以下代码:getSupportActionBar().hide(); 或者
如果您想要没有标题的操作栏,请在您的活动类中使用以下代码:getSupportActionBar().setTitle("");

在setContentView之前还是之后? - serkan stack
在设置 setContentView 后 - BHARADWAJ Marripally

0

标题

在您的活动中使用此代码

supportActionBar?.setDisplayShowTitleEnabled(false)

注意:如果导航图中的片段具有label标签,则此标题不会显示。

NavHostFragment标签

如果您在片段中使用NavHostFragment,则上述解决方案不会完全禁用标题。您将看到片段标签。要禁用它们,请从导航图中删除标签。 之前:

<navigation 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/nav_graph"
    app:startDestination="@id/myFragment">
    <fragment
        android:id="@+id/myFragment"
        <!-- Remove the label below -->
        android:label="MyFragment" 
        android:name="com.example.appName.fragments.MyFragment"
        tools:layout="@layout/my_fragment" />
</navigation>

之后:

<navigation 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/nav_graph"
    app:startDestination="@id/myFragment">
    <fragment
        android:id="@+id/myFragment"
        android:name="com.example.appName.fragments.MyFragment"
        tools:layout="@layout/my_fragment" />
</navigation>

问题: 我可以在活动和片段中使用同一个标题吗?
答案: 不能禁用标题,但可以禁用片段标签以在活动和片段中使用相同的标题。

0

你可以在主活动中使用这个。

ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("");

0
我通过删除MainActivity.java中的

解决了我的问题:


toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

从activity_main.xml中删除工具栏 感谢所有的回复。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接