是否可以为我们添加的android: drawablleft图像设置边缘或填充?
当前回答
是的。使用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" />
其他回答
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只会在文本和可绘制对象之间创建一个填充间隙,如果按钮足够小,可以将两者挤压在一起。如果你的按钮比组合宽度(用于drawablleft /drawableRight)或高度(用于drawableTop/drawableBottom)更宽,那么drawablePadding不会做任何事情。
我现在也在为这个问题挣扎。我的按钮很宽,图标挂在按钮的左边缘,文本在中间居中。我唯一的方法来解决这个问题,现在已经烘焙在可绘制的边缘,通过添加空白像素到画布的左边缘用photoshop。不理想,也不推荐。但这是我目前的权宜之计,短的重建TextView/按钮。
如果可绘制资源的大小是固定的,你可以这样做:
<Button
android:background="@drawable/rounded_button_green"
style="?android:attr/selectableItemBackground"
android:layout_height="wrap_content"
app:layout_widthPercent="70%"
android:drawableRight="@drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
tools:text="example" />
这里的关键是:
android:drawableRight="@drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
也就是说,可绘制资源的大小加上paddingRight等于paddingLeft。
您可以在本例中看到结果
我也会把我的答案扔到擂台上。如果您希望以编程方式完成此操作,可以执行以下操作。
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);
这将添加填充到可绘制的结束,结束将意味着左/右取决于电话是在LTR或RTL。
重新制作:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"/>
<solid android:color="@android:color/white"/>
</shape>
to
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:right="@dimen/_2dp"
android:left="@dimen/_2dp"
android:bottom="@dimen/_2dp"
android:top="@dimen/_2dp"
>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"/>
<solid android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。