简体   繁体   English

如何在折叠工具栏中设置导航箭头并设置Onclicklistener

[英]How to set Navigation arrow in Collapsing Toolbar and set Onclicklistener

I was implementing an collpasing toolbar for my Activity. 我正在为“活动”实现一个折叠工具栏。 I was wondering if it is possible to set and navigation arrow to the Collapsing toolbar layout and set Onclick listener which will take me to previous activity. 我想知道是否可以设置和导航箭头到“折叠”工具栏布局,并设置Onclick侦听器,这将带我进入上一个活动。 I can sucessfully implement such feature using only toolbar layout which is given as 我可以仅使用工具栏布局成功实现此类功能,如下所示

  <android.support.v7.widget.Toolbar android:id="@+id/toolbar_details" android:title="Disclaimer" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:minHeight="56dp" android:layout_alignParentTop="true" app:theme="@style/ThemeOverlay.AppCompat.Light"/> 

and in OnCreate Method, I use 在OnCreate方法中,我使用

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); setSupportActionBar(toolbar); tutorProfile.this.setTitle(getIntent().getStringExtra("My Dashboard")); toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent check = new Intent(CurrentActivity.this, PreviousActivity.class); startActivity(check); finish(); } }); 

But in Collpasing toolbar layout how to achive same function. 但是在Collpasing工具栏布局中如何实现相同的功能。 Here is the xml layout 这是xml布局

 <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing" android:layout_width="match_parent" android:layout_height="150dip" android:fitsSystemWindows="true" app:contentScrim="#784242" app:expandedTitleMarginBottom="20dp" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView app:layout_collapseMode="parallax" android:src="@drawable/feedback_draw" android:contentDescription="@string/app_name" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="200dp" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="56dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> 

I searched for couple of minutes but couldn't find any solution. 我搜索了几分钟,但找不到任何解决方案。 Thanks for help in advance. 预先感谢您的帮助。

Why don't you simple use toolbar.setDisplayHomeAsUpEnabled(true) and then 为什么不简单使用toolbar.setDisplayHomeAsUpEnabled(true) ,然后

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        android.R.id.home -> {
            finish()
            return true
        }
    }
    return super.onOptionsItemSelected(item)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM