Android ActionBar:HomeAsUp具有自定义视图

Android ActionBar:HomeAsUp具有自定义视图,android,android-actionbar,Android,Android Actionbar,我已经为ActionBar设置了自定义视图,我想启用homeAsUp。一切正常,向上指示灯可点击。现在我想让自定义视图也可以点击和触摸(就像普通的应用程序图标一样)。因此,我想将状态按下(悬停)效果也应用于自定义视图。理想情况下,向上指示器和自定义视图显示为一个区域。因此,当我单击/触摸向上指示灯时,自定义视图也应更改其选择器,反之亦然。有没有一个简单的方法 顺便说一句:我不能将应用指示器作为ActionBar自定义视图的一部分,因为我使用ActionBarDrawerToggle…您可以使用a

我已经为ActionBar设置了自定义视图,我想启用homeAsUp。一切正常,向上指示灯可点击。现在我想让自定义视图也可以点击和触摸(就像普通的应用程序图标一样)。因此,我想将状态按下(悬停)效果也应用于自定义视图。理想情况下,向上指示器和自定义视图显示为一个区域。因此,当我单击/触摸向上指示灯时,自定义视图也应更改其选择器,反之亦然。有没有一个简单的方法


顺便说一句:我不能将应用指示器作为ActionBar自定义视图的一部分,因为我使用ActionBarDrawerToggle…

您可以使用appcompat支持库中支持的“工具栏”来完成此操作。

首先,您必须为自定义操作栏创建布局,以便根据您的需求定义功能。这是xml文件

自定义操作栏的布局:

希望这有助于

    <ImageView
        android:id="@+id/custom_actionbar_back_iv"
        android:layout_width="@dimen/small_margin_ar"
        android:layout_height="@dimen/very_small_margin_ar"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/up_navigation"/>

    <TextView
        android:id="@+id/custom_actionbar_titleText_tv"
        style="@style/wrapscreen"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/custom_actionbar_back_iv"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white"
        android:textSize="@dimen/above_medium_text_size" />

    <ImageButton
        android:id="@+id/custom_actionbar_goToArchiveActivity_ib"
        style="@style/wrapscreen"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/medium_margin"
        android:background="@null"
        android:src="@drawable/ic_action_overflow" />

</RelativeLayout>
    private void setUpActionBar() {
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);

            LayoutInflater layoutInflater = LayoutInflater.from(this);
            View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null);

            // this view is used to show tile
            TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv);
            ActivityTitleTV.setText(R.string.title_text_archive);

            //this view can be used for back navigation
            ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv);
            backToRecorderIV.setVisibility(View.VISIBLE);
            backToRecorderIV.setOnClickListener(this);

            //Another view which has up navigation
            ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib);
            goToArchiveActivityIB.setVisibility(View.GONE);

            actionBar.setCustomView(customActionBarView);
            actionBar.setDisplayShowCustomEnabled(true);
        }

        //listener for back navigation
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.custom_actionbar_back_iv:
                    Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class);
                    startActivity(recorder);
                    finish();
                    break;
            }
        }