在工具栏上的子元素的顶部添加后退按钮 [英] Adding backbutton on top of child element of Toolbar

查看:237
本文介绍了在工具栏上的子元素的顶部添加后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局文件。如何添加在顶部的后退按钮(箭头)(或叠加,喜欢的z-index)工具栏的ImageView的孩子在我下面的布局?

I have the following Layout file. How can I add a back button (arrow) on top of (or overlayed on, like a z-index) the Imageview child of Toolbar in my below layout?

<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(真); 并覆盖背面按钮操作可以选择覆盖onOptionItemSelected方法。寻找给定code它会帮助你...:D

You have to set home button of toolbar enable by the following line getSupportActionBar().setDisplayHomeAsUpEnabled(true); and to override the action on back button you have to override onOptionItemSelected method. look for the given code it will help you... :D

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

要覆盖OnOptionItemSelected使用以下code

To override the OnOptionItemSelected use the following code

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        break;

    default:
        break;
    }
        return super.onOptionsItemSelected(item);
    }

这篇关于在工具栏上的子元素的顶部添加后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆