删除工具栏标题

时间:2016-10-15 12:05:38

标签: android android-toolbar

MainActivity:

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>

最后,如何删除android中的标题栏?

6 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

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

答案 1 :(得分:0)

如果您想要隐藏总操作栏,请使用此行在您的活动类中, &#34;的 getSupportActionBar()隐藏(); &#34;

如果您想要带有标题的操作栏,请使用此活动在您的活动类中, &#34;的 getSupportActionBar()的setTitle(&#34;&#34); &#34;

答案 2 :(得分:0)

您可以在主要活动中使用它

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

答案 3 :(得分:0)

我通过删除

解决了我的问题 MainActivity.java中的

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

从actvity_main.xml删除工具栏 感谢您的回复。

答案 4 :(得分:0)

我无法以建议的方式删除标题,因此我通过删除导航图文件中的public function onUserAuthenticate($credentials, $options, &$response) { $db = JFactory::getDbo(); $query = $db->getQuery(true) ->select('id') ->from('#__users') ->where('username=' . $db->quote($credentials['username'])); $db->setQuery($query); $result = $db->loadResult(); if (!$result) { $response->status = STATUS_FAILURE; $response->error_message = 'User does not exist'; } /** * To authenticate, the username must exist in the database, and the password should be equal * to the reverse of the username (so user joeblow would have password wolbeoj) */ if($result && ($credentials['username'] == strrev( $credentials['password'] ))) { $email = JUser::getInstance($result); // Bring this in line with the rest of the system $response->email = $email->email; $response->status = JAuthentication::STATUS_SUCCESS; } else { $response->status = JAuthentication::STATUS_FAILURE; $response->error_message = 'Invalid username and password'; } } 属性解决了我的问题。

例如:

android:label

答案 5 :(得分:0)

有两种处理方法:

  • 以编程方式:
getSupportActionBar().setDisplayShowTitleEnabled(false)
  • 来自XML(样式)
<item name="windowActionBar">false</item>

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