2015-09-23 7 views
0

Я стараюсь, чтобы мое приложение соответствовало новым API, но у меня много проблем с изменением старого ActionBar на панели инструментов. Это просто не появляется. Вот мой MainActivity:Панель инструментов не отображается, нет ошибок

public class MainActivity extends AppCompatActivity implements 
    ActionBar.TabListener, FragmentDrawer.FragmentDrawerListener { 

    private Toolbar mToolbar; 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mToolbar = (Toolbar) findViewById(R.id.toolbar); 

    setSupportActionBar(mToolbar); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 

public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    super.onCreateOptionsMenu(menu); 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 

    super.onOptionsItemSelected(item); 

    switch (item.getItemId()) { 
     case R.id.action_search: 

      break; 
     case R.id.settings: 

      break; 
    } 

    return super.onOptionsItemSelected(item); 
} 

Это файл меню XML:

<menu 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" 
    tools:context="com.adrissa.klea.MainActivity" > 

    <item 
     android:id="@+id/action_search" 
     android:title="@string/action_search" 
     android:icon="@drawable/ic_action_search" 
     app:showAsAction="ifRoom" /> 

    <item 
     android:id="@+id/settings" 
     android:icon="@drawable/ic_action_settings" 
     android:title="@string/settings" 
     app:showAsAction="ifRoom"/> 



</menu> 

Это файл activity_main XML:

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    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:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar"/> 
    <FrameLayout 
     android:id="@+id/container_body" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.adrissa.klea.FragmentDrawer" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

Мой styles.xml

<resources> 

    <style name="MyTheme" parent="@style/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> 

И, наконец, файл toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

Я вижу, что я не единственный, кто испытывает трудности реализации панели инструментов вместо ActionBar, но я, кажется, не найти какой-либо из предложенных решений, в соответствии со своими проблемами. У меня нет ошибок. Просто нет панели инструментов.

ответ

0

Вы должны изменить style.xml, что-то вроде этого:

<resources> 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/ColorPrimary</item> 
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
    </style> 
</resources> 

parent = "Theme.AppCompat.NoActionBar"

также вы можете добавить свой включать внутри LinearLayout, например:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:orientation="vertical"> 
    <!-- up bar --> 
    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 
</LinearLayout> 

также попробовать это в toolbar.xml:

android:layout_height="?attr/actionBarSize"

+0

Извините, мой плохой, забыл добавить мои стили.xml. сделали сейчас. –

+0

см. Мое редактирование @ KæmpeKlunker – Vasilisfoo

+0

К сожалению, это не работает :( –

Смежные вопросы