android:drawableLeft 边距和/或填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3337371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:48:27  来源:igfitidea点击:

android:drawableLeft margin and/or padding

androidandroid-layoutandroid-drawable

提问by androidNewbies

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

是否可以为我们添加的图像设置边距或填充android:drawableLeft

回答by Jordan

As cephus mentioned android:drawablePaddingwill only force padding between the text and the drawable if the button is small enough.

正如 cephus 所提到android:drawablePadding的,如果按钮足够小,只会在文本和可绘制对象之间强制填充。

When laying out larger buttons you can use android:drawablePaddingin conjunction with android:paddingLeftand android:paddingRightto force the text and drawable inward towards the center of the button. By adjusting the left and right padding separately you can make very detailed adjustments to the layout.

布置较大的按钮时,您可以android:drawablePadding结合使用android:paddingLeftandroid:paddingRight强制文本和可绘制向按钮的中心向内。通过分别调整左右内边距,您可以对布局进行非常详细的调整。

Here's an example button that uses padding to push the text and icon closer together than they would be by default:

这是一个示例按钮,它使用填充将文本和图标推到比默认情况下更靠近的位置:

<Button android:text="@string/button_label" 
    android:id="@+id/buttonId"
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:layout_gravity="center"
    android:textSize="13dip"
    android:drawableLeft="@drawable/button_icon"
    android:drawablePadding="2dip"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:singleLine="true"
    android:gravity="center" />  

回答by Cheryl Simon

TextView has an android:drawablePaddingproperty which should do the trick:

TextView 有一个android:drawablePadding属性,它应该可以解决问题:

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.

android:drawablePadding

可绘制对象和文本之间的填充。

必须是一个维度值,它是一个附加单位的浮点数,例如“14.5sp”。可用单位有:px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)、mm(毫米)。

这也可能是对包含此类型值的资源(以“@[package:]type:name”形式)或主题属性(以“?[package:][type:]name”形式)的引用.

这对应于全局属性资源符号 drawablePadding。

回答by cephus

android:drawablePaddingwill only create a padding gap between the text and the drawable if the button is small enough to squish the 2 together. If your button is wider than the combined width (for drawableLeft/drawableRight) or height (for drawableTop/drawableBottom) then drawablePadding doesn't do anything.

android:drawablePadding如果按钮足够小以将 2 挤压在一起,则只会在文本和可绘制对象之间创建填充间隙。如果您的按钮比组合宽度(对于 drawableLeft/drawableRight)或高度(对于 drawableTop/drawableBottom)宽,那么 drawablePadding 不会做任何事情。

I'm struggling with this right now as well. My buttons are quite wide, and the icon is hanging on the left edge of the button and the text is centered in the middle. My only way to get around this for now has been to bake in a margin on the drawable by adding blank pixels to the left edge of the canvas with photoshop. Not ideal, and not really recommended either. But thats my stop-gap solution for now, short of rebuilding TextView/Button.

我现在也在为此苦苦挣扎。我的按钮很宽,图标挂在按钮的左边缘,文字居中居中。我目前解决这个问题的唯一方法是使用 photoshop 在画布的左边缘添加空白像素,从而在可绘制对象上留出一个边距。不理想,也不推荐。但这就是我目前的权宜之计,没有重建 TextView/Button。

回答by Parinda Rajapaksha

Yes. use drawablePaddingas follows,

是的。使用drawablePadding如下,

<TextView
        android:id="@+id/tvHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Settings and Contents"
        android:drawableLeft="@drawable/icon_success"
        android:drawablePadding="10dp" />

回答by The Finest Artist

Make your drawable resources.xml

制作可绘制的 resources.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <inset android:drawable="@drawable/small_m" android:insetLeft="10dp" android:insetTop="10dp" />
    </item>
    <item>
        <inset android:drawable="@drawable/small_p" android:insetLeft="10dp" android:insetTop="10dp" />
    </item>
</selector>

回答by Bhargav Thanki

android:drawablePaddingis the easiest way to give padding to drawable icon but You can not give specific one side padding like paddingRightor paddingLeftof drawable icon.To achieve that you have to dig into it. And If you apply paddingLeftor paddingRightto Edittextthen it will place padding to entire Edittextalong with drawable icon.

android:drawablePadding是给填充到绘制图标的最简单的方法,但你不能给出具体的一面填充像paddingRightpaddingLeft的绘制icon.To实现,你要挖进去。如果你申请paddingLeftpaddingRight到,Edittext那么它会将填充Edittext与可绘制图标一起放置到整个。

回答by Umesh N

define a shape for your edittext and give it a padding For Example

为您的编辑文本定义一个形状并给它一个填充例如

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <padding
        android:left="5dp"
        android:right="5dp"
    />
    <solid android:color="#F6F6F6" />
    <stroke
        android:width="1px"
        android:color="#C3C3C3" />

    <corners
        android:bottomLeftRadius="1dp"
        android:bottomRightRadius="1dp"
        android:topLeftRadius="1dp"
        android:topRightRadius="1dp" />
</shape>

The padding defined in this shape will help in give padding to drawableleft or right ---------------------- Apply this shape on EditView

在此形状中定义的填充将有助于为 drawableleft 或 right 提供填充 ---------------------- 在 EditView 上应用此形状

 <EditText
            android:id="@+id/example"
            android:layout_width="fill_parent"
            android:layout_height="36dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/shape2"
            android:drawableLeft="@drawable/icon1"
            android:drawablePadding="@dimen/txtDrwblPadding"
            android:ems="10"
         />

using that defined shape as background will give your EditText some style plus margin to drawableLeft.

使用该定义的形状作为背景将为您的 EditText 提供一些样式以及 drawableLeft 的边距。

回答by Sekhon123

android:drawablePadding is the easiest way to give padding to drawable icon but You can not give specific one side padding like paddingRight or paddingLeft of drawable icon.To achieve that you have to dig into it. And If you apply paddingLeft or paddingRight to Edittext then it will place padding to entire Edittext along with drawable icon.

android:drawablePadding 是为可绘制图标提供填充的最简单方法,但是您不能提供特定的一侧填充,例如可绘制图标的 paddingRight 或 paddingLeft。要实现这一点,您必须深入研究它。并且如果您将 paddingLeft 或 paddingRight 应用于 Edittext,那么它会将填充与可绘制图标一起放置到整个 Edittext。

<TextView android:layout_width="match_parent"
    android:padding="5sp"
    android:id="@+id/date"
    android:gravity="center|start"
    android:drawableEnd="@drawable/ic_calendar"
    android:background="@drawable/edit_background"
    android:hint="Not Selected"
    android:drawablePadding="10sp"
    android:paddingStart="10sp"
    android:paddingEnd="10sp"
    android:textColor="@color/black"
    android:layout_height="wrap_content"/>

回答by u9652784

<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.

注意:layout_width 需要为 wrap_content 并使用 paddingLeft paddingRight drawablePadding 来控制间隙。如果你指定 layout_width 值,图标和文本之间会有间隙,我认为一旦给 layout_width 一个指定值,填充就会测量。

回答by Daniel Ochoa

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.

这会将填充添加到可绘制对象的末尾,其中 end 表示左/右,具体取决于手机处于 LTR 还是 RTL。