android - ActionButton minWidht ignored in some devices -
i'm using actionbarsherlock in project, i'm displaying 3 action buttons custom minwidth. here code:
menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item1" android:showasaction="always" android:title="@string/empty" android:icon="@drawable/item1_selector" /> <item android:id="@+id/menu_item2" android:showasaction="always" android:title="@string/empty" android:icon="@drawable/item2_selector" /> <item android:id="@+id/menu_item3" android:showasaction="always" android:title="@string/empty" android:icon="@drawable/item3_selector" /> </menu>
and styles
values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="apptheme" parent="theme.sherlock.light"> <item name="android:windowbackground">@android:color/white</item> <item name="actionbaritembackground">@color/white</item> <item name="actionbuttonstyle">@style/customactionbuttonstyle</item> </style> <style name="customactionbuttonstyle" parent="widget.sherlock.actionbutton"> <item name="android:minwidth">32dip</item> <item name="android:padding">0dip</item> </style> </resources>
values-v14/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="apptheme" parent="theme.sherlock.light"> <item name="android:windowbackground">@android:color/white</item> <item name="android:actionbaritembackground">@color/white</item> <item name="actionbaritembackground">@color/white</item> <item name="android:actionbuttonstyle">@style/customactionbuttonstyle</item> <item name="actionbuttonstyle">@style/customactionbuttonstyle</item> </style> </resources>
this code not working galaxy s2 android 4.1.2 (hdpi) , galaxy s3 mini android 4.1.2 (hdpi). in devices action buttons seem have default width(56dip) space between them looks bigger. in galaxy s3 same android version works ok, style applied. code works on many devices different android versions , screen densities.
how can achieve set minwidth or reducing space consistently many different devices, there way programatically?
finally ended setting custom action view (imagebutton) every item in action bar. first changed menu.xml removing icon field.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item1" android:showasaction="always" android:title="@string/empty" /> <item android:id="@+id/menu_item2" android:showasaction="always" android:title="@string/empty" /> <item android:id="@+id/menu_item3" android:showasaction="always" android:title="@string/empty" /> </menu>
then created custom layout action button applying existent button style (custom_action_button.xml)
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_small" android:clickable="true"> <imagebutton android:id="@+id/custom_action_bar_button" style="@style/customactionbuttonstyle" android:src="@drawable/some_drawable" android:clickable="false" android:focusable="true"/> </framelayout>
finally in activity (oncreateoptionsmenu method) did:
getsupportmenuinflater().inflate(r.menu.main_menu, menu); final menuitem item = menu.finditem(r.id.menu_item1); layoutinflater li = layoutinflater.from(this); view actionview = li.inflate(r.layout.custom_action_button, null); imagebutton menuitembutton = (imagebutton) actionview.findviewbyid(r.id.custom_action_bar_button); item.setactionview(actionview); //...and on other 2 buttons //setting listener button item.getactionview().setonclicklistener(new onclicklistener() { @override public void onclick(view view) { //do stuff } });
in way able set spacing between buttons consistently in devices using customactionbuttonstyle.
Comments
Post a Comment