2014-11-08 90 views
6

我目前正在尝试将我的应用程序转换为棒棒糖材质设计,并遇到了与操作栏/工具栏有关的一些问题。按照我所做的方式执行所有操作,操作栏/工具栏不会显示在棒棒糖或kitkat设备上。也许有人可以看看我的主题,样式,activity_main(我拿着一堆片段,是唯一放置工具栏xml代码的地方)和mainactivity。ActionBar /工具栏不显示在棒棒糖版本的应用程序

谢谢。

值/ 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> 

值/的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> 

mainactivity

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. 

回答

4

更改你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" > 

    <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> 

,如果你正在使用setSupportActionBar然后toolbar.inflateMenu将无法正常工作。

欲了解更多详情检查这个职位Android Usages Of Toolbar。它可能会帮助你。

+0

爆炸!谢谢! – sanic 2014-11-08 18:16:53

+0

最糟糕的答案。其他人是否应该现在对这两个文件进行区分? – 2016-10-30 16:08:08

4

使用新的AppCompat库时,您的活动必须从ActionBarActivity延伸。

+0

已经这样做了,也实现onitemclicklistener 我添加了该代码进行澄清。 – sanic 2014-11-08 05:17:18

+0

@ shamu11您是否尝试将您的父主题更改为“Theme.AppCompat.Light.NoActionBar”您已经在布局中声明工具栏,因此应该没问题。 – 2014-11-08 05:19:54

+1

其实我只是改变了从真到假,现在我有一个操作栏!你能推荐一种方法去修补这个动作栏并添加切换到它吗?谢谢! – sanic 2014-11-08 05:25:56

1

是的,我浪费了一个多小时来解决这个问题。

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

这会自动同步并最终显示toogle图标。

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

这对我有用,希望有帮助。

相关问题