gpt4 book ai didi

android - 如何制作没有工具栏的标签

转载 作者:行者123 更新时间:2023-11-29 20:22:19 25 4
gpt4 key购买 nike

我通过示例制作了两个选项卡,但也有一个工具栏。我怎样才能删除它?看我的MainActivity.java和activity_main.xml

package com.example.andrew.tabstest;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {"Home", "Events"};
int Numboftabs = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Creating The Toolbar and setting it as the Toolbar for the activity

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


// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return ContextCompat.getColor(MainActivity.this, R.color.tabsScrollColor);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}

}

我的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>

<com.example.andrew.tabstest.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/сolorPrimary"/>

<android.support.v4.view.ViewPager
android:id="@+id/pager"

android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>



</LinearLayout>

我试图剪切这部分,但工具栏区域只是变成红色并且没有被删除。

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

最佳答案

在您的 onCreate 方法中使用此代码

ActionBar bar = getSupportActionBar();
if(bar != null) {
bar.hide();
}

检查null是因为不是每个activity都可能有actionbar,所以为了安全起见。你可以跳过它,只使用简洁的

getSupportActionBar().hide();

默认主题应用 ActionBar,因此这就是即使您没有应用自定义工具栏也会获得 ActionBar 的原因。

关于android - 如何制作没有工具栏的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991843/

25 4 0