【问题标题】:Android Toolbar problems with appcompat-v7:22.2.1 NavigationOnClick and windowNoTitleappcompat-v7:22.2.1 NavigationOnClick 和 windowNoTitle 的 Android 工具栏问题
【发布时间】:2015-08-08 07:20:27
【问题描述】:

所以这里有很多关于 StackOverflow 的帖子解决了 windowNoTitle 和 NavigationOnClickListener 的问题,但是所有这些帖子都对我不起作用。我的活动应该使用工具栏而不是默认工具栏来处理返回到父活动的导航(单击导航图标时)。问题是,通过我的主题设置和自定义工具栏,我似乎无法触发导航的点击监听器......

这是我的工具栏(我在主布局中使用 include layout="@layout/toolbar.xml"):

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

这是我在 values.xml 中的主题:

<style name="AppThemeBlue"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/material_blue</item>
    <item name="colorPrimaryDark">@color/material_blue_dark</item>
    <item name="colorAccent">@color/material_green</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/material_blue</item>
    <item name="android:textColor">@color/android:white</item>
</style>

AndroidManifest.xml:

    <activity
        android:name=".skeleton.activities.SignupActivity"
        android:label="@string/title_activity_signup"
        android:parentActivityName=".skeleton.activities.StartActivity"
        android:theme="@style/AppThemeBlue"
        android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.thorrism.skeleton.activities.StartActivity" />
    </activity>

以及我的活动中使用它的代码:

private void setupToolbar(){
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    if(mToolbar != null){
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Test", "test");
                finish();
            }
        });

    }
}

我正在为 gradle 运行以下命令:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
}

首先,标题并没有消失,我尝试添加 android: 作为 windowNoTitle 的前缀,但仍然不起作用。其次,导航图标不会被触发,即使尝试在 onOptionsItemSelected() 中捕获菜单项 android.R.id.home。

请帮助 :( 如果这有任何改变,这不适用于我的 Galaxy S6

【问题讨论】:

    标签: android navigation toolbar


    【解决方案1】:

    工具栏没有收到点击事件的原因可能是因为它是布局中的第一个元素,即

    <RelativeLayout
       ...
       .../>
    
       <include layout="@layout/toolbar"/>
    
       <LinearLayout
           ...
           .../>
    </RelativeLayout>
    

    所以发生的情况是 LinearLayout 的顶部填充可能绘制在工具栏上。因此,LinearLayout 接收 onClick 事件而不是工具栏。要解决此问题,请将工具栏的 include 添加为布局中的最后一个元素,然后它将接收点击。

    来源:Android AppCompat Toolbar does not respond to touching action items

    至于标题仍然显示,即使使用 windowNoTitle,我也不知道为什么这不起作用。一个快速的解决方案是添加:

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    

    在其他人发现问题之前的临时解决方案:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-26
      • 2014-12-22
      • 2015-05-25
      • 2014-12-14
      • 1970-01-01
      • 2015-01-29
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多