Android 如何制作没有 Actionbar 和 Toolbar 的后退按钮?

Jun*_*gHa 4 android android-studio

我在 res/values/style.xml 中制作了这样的主题并应用于删除 ActionBar。

<style name="AppTheme.NoActionbar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">false</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

我做了一个 LinerLayout 看起来像一个带有标题和按钮的 ActionBar(它将具有返回功能)。

我想上传图片,但目前还无法上传。

这是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>
Run Code Online (Sandbox Code Playgroud)

如何在按钮中添加后退功能(id:backToMain)?

Amr*_*iya 7

只需在 java 中绑定 ImageButton 的 id 并在该 id 上设置 clicklistner 即可,如下所示

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