我有以下布局文件。如何在下面的布局中的工具栏的 Imageview 子项顶部(或覆盖在 z-index 上)添加后退按钮(箭头)?
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Toolbar
android:layout_width="fill_parent"
android:layout_height="600dp"
android:id="@+id/toolbar"
android:background="#313B45"
android:weightSum="1"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/headerimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="@+id/textView"
android:scaleType="fitXY"
android:layout_gravity="left|top"
android:layout_weight="1" />
</LinearLayout>
</Toolbar>
您必须通过以下行设置工具栏启用的主页按钮getSupportActionBar().setDisplayHomeAsUpEnabled(true);要覆盖后退按钮上的操作,您必须覆盖 onOptionItemSelected 方法。寻找给定的代码它将帮助你......:D
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
要覆盖 OnOptionItemSelected 使用以下代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)