Java Android删除操作栏标题保留工具栏菜单
我需要在我的应用程序中保留我的标签工具栏来删除actionbar。现在的情况是这样的: 但我希望它看起来像这样: 我的代码xml是:Java Android删除操作栏标题保留工具栏菜单,java,android,tabs,android-actionbar,toolbar,Java,Android,Tabs,Android Actionbar,Toolbar,我需要在我的应用程序中保留我的标签工具栏来删除actionbar。现在的情况是这样的: 但我希望它看起来像这样: 我的代码xml是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation=
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new myFragment1();
case 1:
default:
return new myFragment2();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "myfragment1";
case 1:
default:
return "myfragment2";
}
}
}
}
我怎样才能做到呢?
感谢您的建议只需将其添加到MainActivity中,即可隐藏工具栏的文本
getSupportActionBar().setTitle("");
要删除工具栏,可以将其可见性设置为
android:visibility="gone"
只需将其添加到MainActivity中,即可隐藏工具栏的文本
getSupportActionBar().setTitle("");
要删除工具栏,可以将其可见性设置为
android:visibility="gone"
以下是您的选择:
1.看起来这里根本不需要工具栏。您可以将其从xml文件中删除并添加此行
getSupportActionBar().hide();
到MyActivity.java,而不是
if (toolbar != null) {
setSupportActionBar(toolbar);
}
2.如果希望保留工具栏以备以后使用,可以将其隐藏在任意一个xml文件中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="?attr/colorPrimary"/>
以下是您的选择:
1.看起来这里根本不需要工具栏。您可以将其从xml文件中删除并添加此行
getSupportActionBar().hide();
到MyActivity.java,而不是
if (toolbar != null) {
setSupportActionBar(toolbar);
}
2.如果希望保留工具栏以备以后使用,可以将其隐藏在任意一个xml文件中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="?attr/colorPrimary"/>
添加以下代码以在以下文件中禁用actionbar和启用工具栏:- 将此代码添加到styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
现在,您已将自定义工具栏添加到android活动中。添加以下代码以禁用actionbar并在以下文件中启用工具栏:- 将此代码添加到styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
现在,您已经在android活动中添加了自定义工具栏。请尝试在layouttry中的工具栏元素上设置visibility=“gone”在layouttoolbar、action bar中的工具栏元素上设置visibility=“gone”它们是同一事物的不同名称:我认为蓝色部分带有“AppName”是工具栏,这些代码行将隐藏它,它至少对我有效谢谢,我以为工具栏中有我需要的选项卡,第一次使用它时我是从互联网上复制的,但我迷路了。。非常感谢:)工具栏,动作栏它们是同一个东西的不同名称:我想带“AppName”的蓝色部分是工具栏,这些代码行会隐藏它,至少对我有用谢谢,我以为工具栏里面有我需要的标签,第一次使用时我从互联网上复制了它,但我迷路了。。非常感谢:)谢谢你的回复,这还可以,我设置另一个作为解决方案只是因为他写了第一个谢谢你的回复,这还可以,我设置另一个作为解决方案只是因为他写了第一个