隐藏应用程序名称重叠' Burger' support.v7.widget.Toolbar上的图像

时间:2014-11-18 20:05:40

标签: android android-support-library android-5.0-lollipop android-toolbar

我正在尝试support.v7.widget.Toolbar。想要在汉堡图像右侧显示我的应用名称。 我尝试了以下代码。

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    // Set the custom toolbar
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    drawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            toolbar,  /* toolbar */
            R.string.empty,  /* "open drawer" description */
            R.string.empty  /* "close drawer" description */
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            supportInvalidateOptionsMenu();
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            supportInvalidateOptionsMenu();
        }
    };

    // Set the drawer toggle as the DrawerListener
    drawerLayout.setDrawerListener(toggle);
    toggle.syncState();

但它将应用名称与汉堡图像重叠。另外,我尝试过toolbar.setTitle(" null")和setDisplayShowTitleEnabled(false)。它仍显示与汉堡图像重叠的应用程序名称。

另外,我的自定义工具栏布局为空。如果我使用toolbar.setTitle(" App Name"),它仍显示2个应用程序名称。 1具有我想要的效果,第2个具有与图像重叠的名称。

 after setDisplayShowTitleEnabled(false);

删除setDisplayShowTitleEnabled(false);

enter image description here  请帮忙。提前谢谢。

这里是来自styles.xml的代码

    <!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDarker</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>

这是我的清单代码

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- Splash screen -->
    <activity
        android:name="com.mayu.viratkohli.activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Home screen -->
    <activity
        android:name="com.mayu.viratkohli.activities.HomeActivity"
        android:screenOrientation="portrait" 
        android:theme="@style/AppTheme">
    </activity>
</application>

以下是我更新的活动布局

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

<!-- The main content view -->

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

    <include layout="@layout/custom_toolbar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/app_name" />
</LinearLayout>
<!-- The navigation drawer -->

<LinearLayout
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="50dp"
    android:background="#457C50" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Left Drawer" />
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

更新了问题,并更正了我的活动持有工具栏布局的布局。使用LinearLayout而不是FrameLayout作为工具栏布局的父视图,这将导致隐藏重叠的应用程序名称。并且不要使用getSupportActionBar()。setDisplayShowTitleEnabled(false),它将阻止从工具栏中隐藏应用程序名称。非常感谢您的回复!

答案 1 :(得分:0)

看起来你还没有禁用ActionBar提供的装饰。如果您使用工具栏,您的主题需要从Theme.AppCompat.NoActionBar或Theme.AppCompat.Light.NoActionBar继承。请参阅下面的样式代码段

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

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

答案 2 :(得分:0)

你可以删除第三个参数&#34; toolbar&#34;在ActionBarDrawerToggle构造中。除了设置工具栏的标题。

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this,           /* host Activity */
        drawerLayout,   /* DrawerLayout object */
        R.string.empty, /* "open drawer" description */
        R.string.empty  /* "close drawer" description */
) {

    /** Called when a drawer has settled in a completely closed state. */
    public void onDrawerClosed(View view) {
        supportInvalidateOptionsMenu();
    }

    /** Called when a drawer has settled in a completely open state. */
    public void onDrawerOpened(View drawerView) {
        supportInvalidateOptionsMenu();
    }
};

toolbar.setTitle("App Name");

除此之外,您最好使用NoActionBar主题:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

layout / activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        (...)

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

</LinearLayout>