How to add padding/margin between icon and Up-Butt

2019-06-27 23:36发布

问题:

I have to set margin of 10dp between up button and icon, 5 dp between icon and Title in default action bar (android 4.2.2), can anyone please suggest how it can be done.

I don't want to create custom action bar as my required is exactly same as default action bar with change in height, and padding between icon, up button and title. Also change in text size.

Can this be achieved using defualt action bar. I am able to change the height of action bar.

回答1:

You can add a padding / margin by getting the ImageView of the icon from the ActionBar.

This is how:

ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams(iconLp);

Edit: Use this code for example in your Activity's onCreate method.