2015-10-07 3 views
0

Я сделал два вкладки на примере, но есть панель инструментов тоже. Как я могу стереть его? Посмотрите на моей 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); 
+0

Пример проекта на самом деле вкладки встраивается в панели инструментов, то есть, если вы удалите панель вкладок придется идти, а , –

+0

Вы удаляете панель инструментов из xml? –

ответ

1

Используйте этот код в ваш OnCreate метод

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

Проверка на нуль, потому что не каждый вид деятельности может иметь ActionBar, так, чтобы быть на безопасной стороне. Вы можете пропустить его и просто использовать краткий

getSupportActionBar().hide(); 

тема по умолчанию применяет ActionBar, и, следовательно, именно поэтому вы получаете ActionBar, даже если вы не претендуете пользовательскую панель инструментов.

+0

спасибо, это работает! – AndrewZ

0

Если вам не нужна панель инструментов просто удалите следующий код:

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

от расположения и от MainActivity удалить

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

Также вы можете использовать App Theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

в вашем стиле.xml

0

Вы можете попробовать удалить android:theme="@style/AppTheme" от вашей активности тега или вы можете изменить его, как:

<style name="AppTheme" parent="Theme.AppCompat"> 
     <item name="android:windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
</style> 
Смежные вопросы