是否可以为我们添加的android: drawablleft图像设置边缘或填充?
当前回答
textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);
addressTitleView.setCompoundDrawablePadding();
其他回答
你应该考虑使用图层列表
创建一个这样的可绘制文件,将其命名为ic_calendar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:right="10dp">
<bitmap android:gravity="center_vertical|left"
android:src="@drawable/ic_calendar_16dp"
android:tint="@color/red"
/>
</item>
</layer-list>
在布局文件下,
<TextView
android:id="@+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_calendar"
android:textColor="@color/colorGrey"
android:textSize="14sp"
/>
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:paddingLeft="-8dp"
如果可绘制资源的大小是固定的,你可以这样做:
<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。
您可以在本例中看到结果
为你的编辑文本定义一个形状,并给它一个填充 例如
<?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>
在此形状中定义的填充将帮助为可绘制的左侧或右侧提供填充 ---------------------- 应用这个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"
/>
使用定义的形状作为背景将给你的EditText一些风格加上边框drawablleft。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。