简体   繁体   English

Android如何在没有操作栏和工具栏的情况下返回按钮?

[英]Android How to make back button without Actionbar and Toolbar?

I made a theme like this in res/values/style.xml and applied to remove ActionBar. 我在res / values / style.xml中制作了这样的主题,并应用于删除ActionBar。

<style name="AppTheme.NoActionbar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">false</item>
    </style>

And I made a LinerLayout looks like a ActionBar with title and a button(it will have back function). 我使LinerLayout看起来像带有标题和按钮的ActionBar(它将具有后退功能)。

I'd like to upload the pictures, but I can't upload them yet. 我想上传图片,但是还不能上传。

This is a part of the layout.xml 这是layout.xml的一部分

<LinearLayout
        android:id="@+id/ActionBarProductInfo"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

            <ImageButton
                android:id="@+id/backToMain"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:background="@drawable/image_back_34dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="210dp"
                android:layout_marginStart="210dp"
                android:text="Input product information"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                android:textStyle="bold" />

    </LinearLayout>

How can I put a back function in the button(id:backToMain)? 如何在按钮(id:backToMain)中添加后退功能?

Simply bind your id of ImageButton in your java and set clicklistner on that id like this 只需将您的ImageButton的ID绑定到Java中,然后在该ID上设置clicklistner,就像这样

ImageButton back = (ImageButton)findViewById(R.id.backToMain);
back.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

You just have to include your layout in activity then bind your back button. 您只需要在活动中包括布局,然后绑定后退按钮即可。

<include layout="@layout/layout" /> 

put this in activity then 然后将此活动

ImageButton back1 = (ImageButton) findViewById(R.id.backToMain);
back1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

First initialize toolbar 首先初始化工具栏

setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

To handle click 处理点击

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getItemId() == android.R.id.home) {
        Timber.d("Home pressed");
    }
    return super.onOptionsItemSelected(menuItem);
}

I Hope this will work for you. 希望这对您有用。

Modify your layout as given below. 如下所示修改您的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ActionBarProductInfo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/colorPrimary"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageButton
    android:id="@+id/backToMain"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:layout_marginLeft="15dp"
    android:background="@mipmap/ic_back" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Input product information"
    android:textColor="@android:color/white"
    android:textSize="20sp"
    android:textStyle="bold" />
 </LinearLayout>

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

相关问题 如何使所有活动(MainActivity除外)同时具有带有活动标题和后退按钮的ActionBar/Toolbar? - How to make all activities(except MainActivity) have ActionBar/Toolbar at once with the title of the Activity and a back button? 如何在动作栏中制作功能后退按钮,例如设备android中的后退按钮 - How to make function back button in actionbar like back button in devices android 如何实现Android ActionBar 后退按钮? - How to implement the Android ActionBar back button? 如何在 Android Studio 的 ActionBar 上添加返回按钮? - How to add back button on ActionBar in Android Studio? 如何使工具栏中的后退按钮位于工具栏的右侧? - How to make the back button in toolbar to the right side of the toolbar? 如何更改 Android 中工具栏后退按钮的颜色? - How to change color of Toolbar back button in Android? 如何实现android工具栏后退按钮 - How to implement android Toolbar Back button Android-如何向工具栏后退按钮添加ID? - Android - How to add id to Toolbar back button? 如何在工具栏android kotlin上添加后退按钮 - how to add back button on toolbar android kotlin 如何使 Toolbar 标题可以像在 ActionBar 上一样点击,而不将其设置为 ActionBar? - How to make the Toolbar title clickable exactly like on ActionBar, without setting it as ActionBar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM