android:drawableLeft margin and/or padding

后端 未结 18 2111
猫巷女王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:52
    textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);
    
    addressTitleView.setCompoundDrawablePadding();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 09:56

    You can use android:drawableLeft="@drawable/your_icon" to set the drawable to be shown on the left side. In order to set a padding for the drawable you should use the android:paddingLeft or android:paddingRight to set the left/right padding respectively.

    android:paddingRight="10dp"
    android:paddingLeft="20dp"
    android:drawableRight="@drawable/ic_app_manager"
    
    0 讨论(0)
  • 2020-11-27 09:58

    Tries to use negative padding

    Like:

    android:paddingLeft="-8dp"
    
    0 讨论(0)
  • 2020-11-27 09:59
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:background="@drawable/a"
        android:drawableLeft="@drawable/concern_black"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:drawablePadding="10dp"
        android:text="text"/>
    

    note: layout_width needs to be wrap_content and use paddingLeft paddingRight drawablePadding to control gap. If you specify layout_width value is will has gap between icon and text, I think once give the layout_width a specify value, the padding will measure.

    0 讨论(0)
  • 2020-11-27 10:01

    TextView has an android:drawablePadding property which should do the trick:

    android:drawablePadding

    The padding between the drawables and the text.

    Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

    This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

    This corresponds to the global attribute resource symbol drawablePadding.

    0 讨论(0)
提交回复
热议问题