簡體   English   中英

Android如何在沒有操作欄和工具欄的情況下返回按鈕?

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

我在res / values / style.xml中制作了這樣的主題,並應用於刪除ActionBar。

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

我使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>

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

只需將您的ImageButton的ID綁定到Java中,然后在該ID上設置clicklistner,就像這樣

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

您只需要在活動中包括布局,然后綁定后退按鈕即可。

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

然后將此活動

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

首先初始化工具欄

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

處理點擊

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

希望這對您有用。

如下所示修改您的布局。

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM