简体   繁体   中英

Android Studio: App Icon

I am working on an app which drops down a menu once the user has pressed the app icon (located on the left of the action bar). However I am having difficulty with making the app icon clickable. The closes I have gotten is using:

actionBar.setDisplayHomeAsUpEnabled()

However this doesn't make my app icon clickable, rather, it applies a left arrow.

Code:

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); 
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher); 

Create a custom view for your actionBar like the following and modify it for your needs:

Java Code:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(android.R.color.transparent);

LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.actonbar_layout, null);

actionBar.setCustomView(view);

ImageView appIcon = (ImageView) view.findViewById(R.id.appIcon);
appIcon.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Open your menu here...
    }
});

actionbar_layout.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:focusable="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:clickable="true"
            android:id="@+id/appIcon"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/appName" />

</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