简体   繁体   中英

Custom Home Button android

Im working on an app and I need to change the home actionbar button to something like this.

**在此处输入图片说明**

So what I did is make a custom layout with two ImageVies, one for the logo and another for the three bars to use it as home button, here is the code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:layout_marginLeft="-10dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="0dp" >

    <ImageView
        android:id="@+id/actionBa"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center_vertical|start"
        android:layout_marginLeft="-7dp"
        android:clickable="true"
        android:focusable="true"
        android:longClickable="true"
        android:src="@drawable/ic_ab_back_holo_dark_am" />

    <ImageView
        android:id="@+id/actionBarLogo"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="-10dp"
        android:layout_marginBottom="2dp"
        android:clickable="true"
        android:focusable="true"
        android:longClickable="true"
        android:src="@drawable/ic_logo" />

I tried this code and got this:

final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.actionbar);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

The tittle disappeared, I can't click on my custom home button and I have no idea how to remove the left padding of the actionbar. Any ideas how to make it work?

在此处输入图片说明

Try like this :

final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_action_bar, null);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setCustomView(actionBarLayout);

Try this code..solve your issue?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:layout_marginLeft="0dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="0dp" >

        <ImageView
            android:id="@+id/actionBa"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="0dp"
            android:clickable="true"
            android:focusable="true"
            android:longClickable="true"
            android:src="@drawable/ic_ab_back_holo_dark_am" />

        <ImageView
            android:id="@+id/actionBarLogo"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="0dp"
            android:clickable="true"
            android:focusable="true"
            android:longClickable="true"
            android:src="@drawable/ic_logo" />

    </LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM