是否可以为我们添加的android: drawablleft图像设置边缘或填充?
当前回答
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是给可绘制图标填充最简单的方法,但你不能给特定的一侧填充,如paddingRight或paddingLeft的可绘制图标。要做到这一点,你必须深入研究。 如果你将paddingLeft或paddingRight应用到Edittext,那么它会将填充到整个Edittext以及可绘制的图标。
textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);
addressTitleView.setCompoundDrawablePadding();
而不是按钮使用LinearLayout与ImageView和TextView里面。在子项目,如ImageView和TextView使用android:duplicateParentState="true"。
重新制作:
<?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>
正如cephus提到的android:drawablePadding只会在按钮足够小的情况下在文本和可绘制对象之间强制填充。
当布局较大的按钮时,你可以使用android:drawablePadding和android:paddingLeft和android:paddingRight来强制文本和可绘制的向内朝向按钮的中心。通过分别调整左右填充,你可以对布局进行非常详细的调整。
下面是一个示例按钮,它使用填充物将文本和图标推得比默认情况下更近:
<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" />
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件