android:drawableLeft margin and/or padding

后端 未结 18 2241
猫巷女王i
猫巷女王i 2020-11-27 09:29

Is it possible to set the margin or padding for the image which we added with the android:drawableLeft?

18条回答
  •  孤独总比滥情好
    2020-11-27 09:54

    I'll throw my answer into the ring as well. If you want to do this programmatically you can do the following.

    final Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.somedrawable);
    final boolean isLTR = ViewCompat.LAYOUT_DIRECTION_LTR == ViewCompat.getLayoutDirection(this);
    final int iconInsetPadding = getResources().getDimensionPixelSize(R.dimen.icon_padding);
    
    final Drawable insetDrawable = new InsetDrawable(drawable, isLTR ? 0 : iconInsetPadding, 0, isLTR ? iconInsetPadding : 0, 0);
    

    This will add the padding to the end of the drawable where end will mean left/right depending if phone is in LTR or RTL.

提交回复
热议问题