Pol
Pol

Reputation: 481

How to apply margin to actionViewClass in actionbar menu

I want to put some padding between my customs actionbar menuitems. I inflate several menuItem such as this one:

 <item android:id="@+id/action_imp_med"
    android:actionViewClass="com.xx.xx.xx.xx.MyImportanceAction"
    app:actionViewClass="com.xx.xx.xx.xx.MyImportanceAction"
    app:showAsAction="always"
    android:title="@string/importance_medium"
    app:ImportancePickerView_importance="med"
    />

Then, in my fragment's "onCreateOptionsMenu" I set the layout params as follow:

ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
        Utils.dpTopx(getActivity(), 25),
        Utils.dpTopx(getActivity(), 25));
lp.setMargins(Utils.dpTopx(getActivity(), 20), 0, 0, 0);
((MyImportanceAction) menuItem.getActionView()).setLayoutParams(lp);

My custom view is well resized. But no way to set the margins. Any Idea ?

MyImportanceAction extends from imageView.

ActionBar is the one from appcompat.

Upvotes: 1

Views: 1061

Answers (2)

Pol
Pol

Reputation: 481

I succeed by wrapping my Customview (which extend from imageview) with a linear layout and added some margin to my custom view in that layout as follow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<com.xx.xx.xx.xx.MyImportanceAction
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_marginLeft="5dp"
    android:layout_gravity="center_vertical"
    />
</LinearLayout>

It is more a workaround than an answer I guess...

Upvotes: 2

phemt.latd
phemt.latd

Reputation: 1803

Try with a custom button style, add following lines in styles.xml file:

 <style name="AppTheme" parent="AppBaseTheme">
     <item name="android:actionButtonStyle">@style/CustomActionButtonStyle</item>
 </style>

 <style name="CustomActionButtonStyle" parent="AppBaseTheme">
     <item name="android:minWidth">25dip</item>
     <item name="android:padding">3dip</item>
 </style>

Adjust the value of padding and width to Achieve your expected behavior.

Upvotes: 0

Related Questions