ActionBar/Toolbar 在 Lollipop 版本的应用中没有显示

6

我正在尝试将我的应用程序转换为Lollipop材料设计,并在操作栏/工具栏方面遇到了一些问题。按照我所做的方式实现,操作栏/工具栏在Lollipop或Kitkat设备上都不会显示。也许有人可以查看我的主题、样式、activity_main(我在其中保存了一堆片段,并且是我放置工具栏XML代码的唯一位置)和MainActivity。

谢谢。

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

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

主活动

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.
3个回答

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。它可能会对您有所帮助。


最糟糕的回答。其他人现在应该对这两个文件进行差异比较吗? - Eugene Pankov

4

已经在做了,同时也在实现onitemclicklistener。我添加了这段代码以作澄清。 - sanic
@shamu11 你尝试过将父主题更改为“Theme.AppCompat.Light.NoActionBar”吗?你已经在布局中声明了工具栏,所以应该没问题。 - Roberto Betancourt
1
其实我只是将<item name="android:windowNoTitle">true</item>中的true改为false,现在我有了一个操作栏!你能推荐一种方法来调整这个操作栏并添加切换吗?谢谢! - sanic
尝试移除以下元素:<item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> - Roberto Betancourt
这个和上面的答案结合起来解决了我的问题,谢谢! - sanic
显示剩余3条评论

1

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

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

这将自动同步并最终显示切换图标。
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

那对我来说可以,希望能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接