简体   繁体   中英

Custom Menu Item on ActionBar

I am now having a problem with using "android:actionLayout" in menu.

I have used this and worked in some of my previous project but now i have no idea what is going on.

When i typed

android:title="@string/menu_notifications"
android:actionLayout="@layout/menu_notification"

Every icon in actionBar in my application changed to Text.

I tried a lot of things nothing have worked. I have no clue what is happening.

Please some advice / idea?

EDIT: menu_notification.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="TEST"
android:textSize="20dp"
android:textColor="#F00"
/>
</LinearLayout>

EDIT 2:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

change menu_notification.xml as,

 <?xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="TEST"
    android:textSize="20dp"
  android:textColor="#F00"
 />

and main.xml,

     <item
     android:id="@+id/log_out"
      android:actionLayout="@layout/menu_notification"
    android:icon="@drawable/power"

 android:showAsAction="always"
/>

then ,

 @Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
TextView txtview=(TextView )menu.findItem(R.id.log_out).getActionView();
//do remaining stuff here

}

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