如何解决TextView drawableRight左侧图片大小不可控的问题

54 篇文章 2 订阅

可以尝试以下两种简单方式来控制 drawableRight 图标的大小:

1,使用 TextView 的 setCompoundDrawablesRelativeWithIntrinsicBounds() 方法来设置 drawableRight 图标,并通过设置 Drawable 对象的 setBounds() 方法来控制图标大小。

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_icon);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);

 2.,通过在 res/drawable 目录下创建vector 矢量图,它可以无限放大或缩小而不失真,用 xml 文件来自定义 drawableRight 图标,并在 TextView 中使用该 drawable。然后在 TextView 中使用该 drawable:可以通过调整 xml 文件中的 android:width 和 android:height 属性来控制图标大小。

比如下图中的箭头图标:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="20dp"
        android:width="20dp"
        android:viewportHeight="1024"
        android:viewportWidth="1024">
    <path
            android:fillColor="#717171"
            android:pathData="M658.56,557.39L322.54,221.38l45.25,-45.26 336.02,336.01zM704.09,512.2L364.12,852.16l-45.26,-45.25 339.98,-339.98z"/>
</vector>

 使用:

            <TextView
                    android:id="@+id/txtShowAll"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="显示全部"
                    android:drawableRight="@drawable/ic_arow_pre_gray"
                    android:textColor="@color/SecondTextGray"/>

限制前大小:

限制后大小:

 当然箭头也可以更小:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值