2014-11-08 14 views
6

Obecnie próbuję przekonwertować moją aplikację na projekt materiału Lollipop i napotkałem problemy z paskiem akcji/paskiem narzędzi. Wdrażając wszystko tak jak ja, pasek akcji/pasek narzędzi nie będzie wyświetlany na urządzeniu Lollipop lub KitKat. Być może ktoś mógłby spojrzeć na moje motywy, style, activity_main (gdzie trzymam garść fragmentów i jest jedynym miejscem, w którym umieszczam kod xml paska narzędzi) i mainactivity.Pasek akcji/pasek narzędzi nie jest wyświetlany w wersji Lollipop aplikacji

Dzięki.

Wartości/styles.xml

<resources> 

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> 
    <!-- Set AppCompat’s actionBarStyle --> 
    <item name="actionBarStyle">@menu/action_menu</item> 

    <!-- The rest of your attributes --> 
</style> 

</resources> 

/wartości themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="AppTheme" parent="AppTheme.Base"/> 

<style name="AppTheme.Base" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/material_blue_grey_800</item> 
    <item name="colorPrimaryDark">@color/material_blue_grey_950</item> 
    <item name="android:textColor">@color/black</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 
</resources> 

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<RelativeLayout 
    android:id="@+id/drawerView" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" /> 

    <ListView 
     android:id="@+id/drawerList" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:background="@drawable/nav_border" 
     android:divider="@null" /> 
</RelativeLayout> 
<!-- 
     <fragment 
     android:id="@+id/fragment1" 
     android:name="com.shamu11.madlibsportable.MadlibsSelect" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

--> 

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

główną działalność

public class MainActivity extends ActionBarActivity implements 
    OnItemClickListener { 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    Fragment fragment = new MadlibsSelect(); 

    fragment = new MadlibsSelect(); 

    FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); 
    fragment.setArguments(getIntent().getExtras()); 
    fm.add(R.id.fragment_container, fragment); 
    fm.commit(); 

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

    toolbar.inflateMenu(R.menu.action_menu); 

    //ActionBar bar = getActionBar(); 
    //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#428bca"))); 

    ...more stuff happen down here including adding nav drawer. 

Odpowiedz

4

Zmień jesteś activity_main.xml następująco

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     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:minHeight="?attr/actionBarSize" /> 

     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/drawerView" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" > 

     <ListView 
      android:id="@+id/drawerList" 
      android:layout_width="250dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="left" 
      android:background="@drawable/nav_border" 
      android:divider="@null" /> 
    </RelativeLayout> 
    <!-- 
     <fragment 
     android:id="@+id/fragment1" 
     android:name="com.shamu11.madlibsportable.MadlibsSelect" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    --> 

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

i Jeśli używasz setSupportActionBar następnie toolbar.inflateMenu nie będzie działać.

Aby uzyskać więcej informacji, proszę sprawdzić ten artykuł Android Usages Of Toolbar. To może ci pomóc.

+0

BANG ON! Dzięki! – sanic

+0

Najgorszy rodzaj odpowiedzi. Czy reszta ludzi ma teraz zrobić diff na tych dwóch plikach? –

4

Twoja aktywność musi rozciągać się od ActionBarActivity podczas korzystania z nowej biblioteki AppCompat.

+0

Już to robiłem, także implementując onitemclicklistener Dodałem ten kod do wyjaśnienia. – sanic

+0

@ shamu11 Czy próbowałeś zmienić motyw macierzysty na "Theme.AppCompat.Light.NoActionBar" Już deklarujesz pasek narzędzi w swoim układzie, więc powinieneś być w porządku. –

+1

Właściwie właśnie zmieniłem true z true na false, a teraz mam pasek akcji! Czy możesz polecić sposób na pójście do majsterkowicza tym paskiem akcji i dodać do niego przełącznik? Dzięki! – sanic

1

Tak, to się stało, zmarnowałem ponad godzinę, żeby to naprawić.

@Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
    } 

Spowoduje to automatyczną synchronizację i ostatecznie wyświetlenie ikony toogle.

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Pass any configuration change to the drawer toggls 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

To działa dla mnie, Nadzieja, która pomaga.

Powiązane problemy