Android appcompat-v7:22.2.1 NavigationOnClick 和 windowNoTitle 的工具栏问题
Android Toolbar problems with appcompat-v7:22.2.1 NavigationOnClick and windowNoTitle
所以 Whosebug 上有很多帖子解决了 windowNoTitle 和 NavigationOnClickListener 的问题,但是所有这些帖子都不适合我。我的 Activity 应该使用工具栏而不是默认工具栏来处理返回到 parent Activity 的导航(当单击导航图标时)。问题是,在我的主题设置和使用自定义工具栏的情况下,我似乎无法触发导航的点击侦听器...
这是我的工具栏(我在主布局中使用 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>
以及我的 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 上不起作用
工具栏没有收到点击事件的原因可能是因为它是布局中的第一个元素,即
<RelativeLayout
...
.../>
<include layout="@layout/toolbar"/>
<LinearLayout
...
.../>
</RelativeLayout>
那么发生的事情是 LinearLayout 的顶部填充可能绘制在工具栏上。因此,LinearLayout 接收的是 onClick 事件,而不是工具栏。要解决此问题,请将工具栏的包含添加到布局中的最后一个元素,然后它将收到点击。
来源:Android AppCompat Toolbar does not respond to touching action items
至于标题仍然显示,即使有 windowNoTitle,我不知道为什么这不起作用。一个快速的解决方案是添加:
getSupportActionBar().setDisplayShowTitleEnabled(false);
在其他人发现错误之前的临时解决方案:)
所以 Whosebug 上有很多帖子解决了 windowNoTitle 和 NavigationOnClickListener 的问题,但是所有这些帖子都不适合我。我的 Activity 应该使用工具栏而不是默认工具栏来处理返回到 parent Activity 的导航(当单击导航图标时)。问题是,在我的主题设置和使用自定义工具栏的情况下,我似乎无法触发导航的点击侦听器...
这是我的工具栏(我在主布局中使用 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>
以及我的 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 上不起作用
工具栏没有收到点击事件的原因可能是因为它是布局中的第一个元素,即
<RelativeLayout
...
.../>
<include layout="@layout/toolbar"/>
<LinearLayout
...
.../>
</RelativeLayout>
那么发生的事情是 LinearLayout 的顶部填充可能绘制在工具栏上。因此,LinearLayout 接收的是 onClick 事件,而不是工具栏。要解决此问题,请将工具栏的包含添加到布局中的最后一个元素,然后它将收到点击。
来源:Android AppCompat Toolbar does not respond to touching action items
至于标题仍然显示,即使有 windowNoTitle,我不知道为什么这不起作用。一个快速的解决方案是添加:
getSupportActionBar().setDisplayShowTitleEnabled(false);
在其他人发现错误之前的临时解决方案:)