如何启用homeAsUp或致电setDisplayHomeAsUpEnabled()的独立工具栏上appcompat V21栏上、独立、工具、setDisplayHomeAsUpEnabled

由网友(生死皆闲事)分享简介:我想将我的应用程序来使用V21 AppCompat库,所以我开始使用工具栏,而不是动作条。在我所有的常规活动(延伸ActionBarActivity)一切都很好。但在我SettingsActivity延伸preferenceActivity,因此,我不能使用setSupportActionBar(动作条)调用我需要使用...

我想将我的应用程序来使用V21 AppCompat库,所以我开始使用工具栏,而不是动作条。在我所有的常规活动(延伸ActionBarActivity)一切都很好。但在我SettingsActivity延伸preferenceActivity,因此,我不能使用setSupportActionBar(动作条)调用我需要使用一个独立的工具栏。工具栏显示出来,但我想不通我怎么可以添加家/向上按钮添加到工具栏。

SettingsActivity.java:

 公共类SettingsActivity扩展preferenceActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_settings);
        工具栏动作条=(工具栏)findViewById(R.id.actionbar);
        如果(NULL!=动作条){

            //在扩展ActionBarActivity我只是利用一切其他活动:
            // setSupportActionBar(动作条);
            //最后的动作条supportActionBar = getSupportActionBar();
            // supportActionBar.setDisplayHomeAsUpEnabled(真正的);

            //但在preferenceActivity你需要添加一个独立的工具栏:
            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    SettingsActivity.this.finish();
                }
            });

            //充气菜单要显示的工具栏
            actionbar.inflateMenu(R.menu.settings);
        }
    }
}
 

布局/ activity_settings.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              的xmlns:工具=htt​​p://schemas.android.com/tool​​s
              工具:SettingsActivity上下文=
              工具:菜单=设置
              工具:actionBarNavMode =标准
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:方向=垂直>

    <包括
        布局=@布局/动作条
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

    <的ListView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:ID =@机器人:ID /列表/>
< / LinearLayout中>
 

布局/ actionbar.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v7.widget.Toolbar
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID /动作条
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =match_parent
    机器人:=了minHeight?ATTR / actionBarSize
    机器人:背景=?ATTR / colorPrimaryDark
    应用程序:主题=@风格/ AppTheme
    />
 
bios选项 cpu xd support 开启还是不开启 limit cpu id value开启还是不开启

菜单/ settings.xml中:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    工具:上下文=com.fletech.android.redalert.SettingsActivity>
< /菜单>
 

我试图actionBarStyle和displayOptions添加到我的主题,在Show在Android嵌套$ P $上/后退按钮pferenceScreen?,但在其他地方的人说,当我使用工具栏actionBarStyle将不被使用,他们似乎是正确的。

值/的themes.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=AppTheme父=AppTheme.Base/>

    <样式名称=AppTheme.Base父=Theme.AppCompat>
        <项目名称=colorPrimary> @色/ colorPrimary< /项目>
        <项目名称=colorPrimaryDark> @色/ colorPrimary< /项目>
        <项目名称=机器人:windowNoTitle>真< /项目>
        <项目名称=windowActionBar>假< /项目>
        <项目名称=windowActionModeOverlay>真< /项目>

        <! - 集AppCompat的actionBarStyle  - >
        <项目名称=actionBarStyle> @风格/ MyActionBar< /项目>
    < /风格>

    <样式名称=MyActionBar父=Widget.AppCompat.ActionBar>
        <项目名称=displayOptions> showHome | homeAsUp | showTitle< /项目>
    < /风格>
<资源>
 

解决方案

@Pedro奥利维拉的解决方案工作。我甚至可以找到绘制的AppCompat库使用(并因此已被包含在APK)。它也反映什么比较多,所以它的工作原理都为LTR,RTL语言环境:

  actionbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
 

和这是它产品总数,从@VictorYakunin修正

 公共类SettingsActivity扩展preferenceActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_settings);
        工具栏动作条=(工具栏)findViewById(R.id.actionbar);
        如果(NULL!=动作条){
            actionbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    NavUtils.navigateUpFromSameTask(SettingsActivity.this);
                }
            });

            //充气菜单要显示的工具栏
            actionbar.inflateMenu(R.menu.settings);
        }
    }
}
 

I am trying to convert my app to use the v21 AppCompat library, so I started to use Toolbar instead of ActionBar. In all my regular activities (which extend ActionBarActivity) everything is fine. but in my SettingsActivity which extends PreferenceActivity, and therefore I can't use the setSupportActionBar(actionbar) call I need to use a "standalone" toolbar. The toolbar shows up, but I can't figure out how could I add the "home / up" button to the toolbar.

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
        if (null != actionbar) {

            // In every other activity that extends ActionBarActivity I simply use:
            // setSupportActionBar(actionbar);
            // final ActionBar supportActionBar = getSupportActionBar();
            // supportActionBar.setDisplayHomeAsUpEnabled(true);

            // but in PreferenceActivity you need to add a standalone toolbar:    
            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SettingsActivity.this.finish();
                }
            });

            // Inflate a menu to be displayed in the toolbar
            actionbar.inflateMenu(R.menu.settings);
        }
    }
}

layout/activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              tools:context=".SettingsActivity"
              tools:menu="settings"
              tools:actionBarNavMode="standard"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <include
        layout="@layout/actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" />
</LinearLayout>

layout/actionbar.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/actionbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    app:theme="@style/AppTheme"
    />

menu/settings.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.fletech.android.redalert.SettingsActivity" >
</menu>

I tried to add actionBarStyle and displayOptions to my theme as explained in Show up/back button on android nested PreferenceScreen?, but in other places people said that actionBarStyle won't be used when I use Toolbar, and they seem to be right.

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/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>

        <!-- Set AppCompat’s actionBarStyle -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
        <item name="displayOptions">showHome|homeAsUp|showTitle</item>
    </style>
<resources>

解决方案

@Pedro Oliveira's solution worked. I could even find the drawable that the AppCompat library uses (and therefore is already included in the apk). What more it's also mirrored, so it works both for ltr, rtl locales:

actionbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

and this is it alltogether, with the correction from @VictorYakunin

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
        if (null != actionbar) {
            actionbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NavUtils.navigateUpFromSameTask(SettingsActivity.this);
                }
            });

            // Inflate a menu to be displayed in the toolbar
            actionbar.inflateMenu(R.menu.settings);
        }
    }
}

阅读全文

相关推荐

最新文章