我正在应用程序中从ActionBar迁移到工具栏。 但我不知道如何显示和设置单击事件在工具栏上的后退箭头,就像我在动作栏上做的那样。
使用ActionBar,我调用mActionbar.setDisplayHomeAsUpEnabled(true)。 但是没有类似的方法。
有没有人遇到过这种情况,并以某种方式找到了解决它的方法?
我正在应用程序中从ActionBar迁移到工具栏。 但我不知道如何显示和设置单击事件在工具栏上的后退箭头,就像我在动作栏上做的那样。
使用ActionBar,我调用mActionbar.setDisplayHomeAsUpEnabled(true)。 但是没有类似的方法。
有没有人遇到过这种情况,并以某种方式找到了解决它的方法?
当前回答
可能有一个更可靠的方法从你的主题获得向上图标(如果不使用工具栏作为你的动作栏):
toolbar.navigationIcon = context.getDrawableFromAttribute(R.attr.homeAsUpIndicator)
为了将主题属性转换为可绘制对象,我使用了一个扩展函数:
fun Context.getDrawableFromAttribute(attributeId: Int): Drawable {
val typedValue = TypedValue().also { theme.resolveAttribute(attributeId, it, true) }
return resources.getDrawable(typedValue.resourceId, theme)
}
其他回答
而Kotlin则变成了:
Xml:
<include
android:id="@+id/tbSignToolbar "
layout="@layout/toolbar_sign_up_in"/>
在活动中:-
setSupportActionBar(tbSignToolbar as Toolbar?)//tbSignToolbar :id of your toolbar
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
如果你正在使用ActionBarActivity,那么你可以告诉Android使用工具栏作为动作栏,如下所示:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
然后调用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
将工作。你也可以在片段中使用它附加到ActionBarActivities,你可以这样使用它:
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
如果你没有使用ActionBarActivities,或者如果你想在工具栏上获得未设置为SupportActionBar的后退箭头,那么你可以使用以下方法:
mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What to do on back clicked
}
});
如果您使用的是android.support. vii .widget。工具栏,然后你应该添加以下代码到你的AppCompatActivity:
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
首先,你需要初始化工具栏:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
然后从操作栏调用后退按钮:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
也许它会帮助别人,我没有在答案中找到我最后做的事情: 使用ActionBarDrawerToggle mDrawerToggle; 在工具栏中显示后退箭头。 mDrawerToggle.setDrawerIndicatorEnabled(假);
如果你想让它在工具栏中显示汉堡:
mDrawerToggle.setDrawerIndicatorEnabled(真正的);
您可以随时在工具栏中添加相对布局或线性布局,并在工具栏中任意位置放置后退图标或关闭图标的图像视图
例如,我在工具栏中使用了相对布局
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_width="match_parent"
android:layout_height="35dp"
android:minHeight="?attr/actionBarSize"
android:nextFocusDown="@id/netflixVideoGridView"
app:layout_collapseMode="pin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Myflix"
android:textAllCaps="true"
android:textSize="19sp"
android:textColor="@color/red"
android:textStyle="bold" />
<ImageView
android:id="@+id/closeMyFlix"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/vector_close" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
它是这样的:
你可以在图像视图中从活动或片段中添加点击监听器。
closeMyFlix.setOnClickListener({
Navigator.instance.showFireTV( activity!!.supportFragmentManager)
})