我正在应用程序中从ActionBar迁移到工具栏。 但我不知道如何显示和设置单击事件在工具栏上的后退箭头,就像我在动作栏上做的那样。
使用ActionBar,我调用mActionbar.setDisplayHomeAsUpEnabled(true)。 但是没有类似的方法。
有没有人遇到过这种情况,并以某种方式找到了解决它的方法?
我正在应用程序中从ActionBar迁移到工具栏。 但我不知道如何显示和设置单击事件在工具栏上的后退箭头,就像我在动作栏上做的那样。
使用ActionBar,我调用mActionbar.setDisplayHomeAsUpEnabled(true)。 但是没有类似的方法。
有没有人遇到过这种情况,并以某种方式找到了解决它的方法?
当前回答
在您想要的活动的清单文件中 添加一个返回按钮,我们将使用属性android: paren战术vityname
<activity
android:name=".WebActivity"
android:screenOrientation="portrait"
android:parentActivityName=".MainActivity"
/>
附注:此属性是在API Level 16中引入的。
其他回答
在AppCompatActivity中,你可以这样做
public class GrandStatActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grand_stat);
}
@Override
public void onResume() {
super.onResume();
// Display custom title
ActionBar actionBar = this.getSupportActionBar();
actionBar.setTitle(R.string.fragment_title_grandstats);
// Display the back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
// Back arrow click event to go to the parent Activity
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return 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)
}
您可以随时在工具栏中添加相对布局或线性布局,并在工具栏中任意位置放置后退图标或关闭图标的图像视图
例如,我在工具栏中使用了相对布局
<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)
})
如果你想要得到工具栏上的后退箭头,而不是设置为你的SupportActionBar:
(kotlin)
val resId = getResIdFromAttribute(toolbar.context, android.R.attr.homeAsUpIndicator)
toolbarFilter.navigationIcon = ContextCompat.getDrawable(toolbar.context, resId)
toolbarFilter.setNavigationOnClickListener { fragmentManager?.popBackStack() }
从属性中获取res:
@AnyRes
fun getResIdFromAttribute(context: Context, @AttrRes attr: Int): Int {
if (attr == 0) return 0
val typedValueAttr = TypedValue()
context.theme.resolveAttribute(attr, typedValueAttr, true)
return typedValueAttr.resourceId
}
有很多方法可以做到这一点,下面是我最喜欢的:
布局:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator" />
活动:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// back button pressed
}
});