自定义TextView的drawable可设置大小

方式一:代码设置

        TextView textView = findViewById(R.id.tv);
        Drawable drawable = getResources().getDrawable(R.drawable.apple);
        drawable.setBounds(0, 0, dp2px(45), dp2px(45));
        textView.setCompoundDrawables(drawable, null, null, null);//分别为上下左右位置的drawable

方式二:自定义TextView

在attrs文件中设置如下自定义属性

<declare-styleable name="TextViewDrawable">
        <attr name="drawable_width" format="dimension" />
        <attr name="drawable_height" format="dimension" />
    </declare-styleable>

public class TextViewDrawable extends android.support.v7.widget.AppCompatTextView {
    private static final int DIRECTION_WIDTH = 0;
    private static final int DIRECTION_HEIGHT = 1;
    private float drawableWidth;
    private float drawableHeight;
    private static final String TAG = TextViewDrawable.class.getSimpleName();

    public TextViewDrawable(Context context) {
        this(context, null);
    }

    public TextViewDrawable(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public TextViewDrawable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawable);
        drawableWidth = array.getDimension(R.styleable.TextViewDrawable_drawable_width, 0);
        drawableHeight = array.getDimension(R.styleable.TextViewDrawable_drawable_height, 0);
        array.recycle();
        if (drawableWidth > 0 && drawableHeight > 0) {
            Drawable[] drawables = getCompoundDrawables();
            setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
        }
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, getSize(left, DIRECTION_WIDTH),
                    getSize(left, DIRECTION_HEIGHT));
        }
        if (right != null) {
            right.setBounds(0, 0, getSize(right, DIRECTION_WIDTH),
                    getSize(right, DIRECTION_HEIGHT));
        }
        if (top != null) {
            top.setBounds(0, 0, getSize(top, DIRECTION_WIDTH),
                    getSize(top, DIRECTION_HEIGHT));
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, getSize(bottom, DIRECTION_WIDTH),
                    getSize(bottom, DIRECTION_HEIGHT));
        }
        setCompoundDrawables(left, top, right, bottom);
    }
    //获取图片的宽高
    private int getSize(Drawable drawable, int direction) {
        if (drawableWidth > 0 && drawableHeight > 0) {
            if (direction == DIRECTION_WIDTH) {
                return (int) drawableWidth;
            } else {
                return (int) drawableHeight;
            }
        } else {
            if (direction == DIRECTION_WIDTH) {
                return drawable.getIntrinsicWidth();
            } else {
                return drawable.getIntrinsicHeight();
            }
        }
    }
}
 

<think>首先,用户的问题是基于之前的引用内容:"我想查找在安卓系统中设置TextViewDrawable大小的方法"。引用[1]到[5]提供了相关信息。 关键点是:用户希望知道如何设置TextViewDrawable大小。引用内容展示了不同的方法: - 引用[1]:提到了android:drawableTop等属性,但说明不能直接控制大小,只能设置padding。 - 引用[2]:关于使用Drawable设置字体颜色,但问题焦点是大小。 - 引用[3]:使用layer-list在XML中设置drawable大小,通过<item>指定width和height。 - 引用[4]:在代码中使用setBounds方法设置Drawable大小,然后通过setCompoundDrawables应用。 - 引用[5]:提到了动态设置drawable大小,使用typedArray获取尺寸。 还需要遵守系统级指令: - 所有行内数学表达式用$...$格式。但这个问题是关于Android开发的,所以可能没有数学表达式。我需要确保如果有,就用正确格式。 - 独立公式用$$...$$单独成段。同样,可能不相关。 - LaTeX语法正确:由于没有数学公式,这应该没问题。 - 使用中文回答。 - 生成相关问题:在回答后,添加相关问题。 - 引用末尾添加标识:在回答中引用的部分末尾加[^数字]。 用户上一次的问题是我的回答:"我想查找在安卓系统中设置TextViewDrawable大小的方法",所以我需要直接回答这个。 结构化回答: 1. **介绍问题**:简要解释TextViewdrawable属性和为什么需要设置大小。 2. **解决方法**:基于引用,提供两种主要方法:XML方法和代码方法。 - XML方法:使用layer-list或自定义drawable资源,如引用[3]。 - 代码方法:在Java/Kotlin中使用setBounds和setCompoundDrawables,如引用[4]。 3. **步骤**:详细描述每种方法如何实现。 4. **注意事项**:提到引用[1]的限制,直接使用android:drawableX只能设置padding,不能改变大小。 5. **总结**:推荐方法。 6. **相关问题**:生成3-5个相关问题。 确保回答真实可靠:基于提供的引用,这些是常见Android开发实践。 最终回答结构: - 开头:确认问题。 - 方法1:XML方式。 - 方法2:代码方式。 - 其他提示。 - 相关问题。 添加引用标识:在提到特定方法时,加[^3]或[^4]等。记住,引用[^1]到[^5]是用户提供的。 例如,当提到引用[3]的XML方法时,加[^3];提到引用[4]的代码方法时,加[^4]。 生成相关问题: 1. 如何在XML中自定义drawable大小? 2. 如何在运行时动态调整drawable大小? 3. 还有其他属性可以控制drawable位置吗? 4. 使用drawable时有哪些常见问题? 5. 如何处理不同屏幕密度的drawable大小? 现在,构建回答。 </think>基于您的问题“在安卓系统中设置TextViewDrawable大小的方法”,我将详细解释实现方式。在Android开发中,TextViewDrawable(如drawableTop、drawableLeft等)默认不能直接通过xml属性控制大小;只能设置padding。但我们可以通过XML自定义drawable资源或代码动态设置来实现。以下是两种主流方法,基于您提供的引用内容(引用[^1]-[^5])。 ### 方法1: 使用XML自定义drawable资源(推荐静态设置) 这种方法通过创建自定义drawable文件来控制大小,适合在布局xml中直接引用。 **步骤:** 1. 在res/drawable目录下创建XML文件(如custom_drawable.xml)。 2. 使用`<layer-list>`和`<item>`元素定义drawable大小,通过android:width和android:height属性设置尺寸,引用原始图片源。 3. 在TextView的xml中,使用android:drawableTop等属性引用此自定义drawable。 **示例代码(基于引用[^3]):** ```xml <!-- res/drawable/custom_drawable.xml --> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置drawable大小,例如宽度45dp、高度45dp --> <item android:width="45dp" android:height="45dp"> <bitmap android:src="@drawable/your_icon" /> <!-- 替换为您的图片资源 --> </item> </layer-list> <!-- 在布局xml中使用 --> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Android" android:drawableTop="@drawable/custom_drawable" /> ``` **优点:** 无需代码,可直接在布局文件中管理,适合固定大小场景。 **缺点:** 尺寸硬编码,不易动态调整[^3]。 ### 方法2: 在代码中动态设置大小(推荐动态调整) 这种方法适用于需要在运行时改变大小的场景,例如根据屏幕尺寸或用户交互调整。使用`setBounds()`方法定义drawable尺寸,然后通过`setCompoundDrawables()`应用到TextView。 **步骤:** 1. 在Java或Kotlin代码中获取drawable资源。 2. 调用`drawable.setBounds(left, top, right, bottom)`设置大小(单位像素),注意必须调用此方法否则图片不显示。 3. 使用`setCompoundDrawables()`或类似方法将drawable绑定到TextView。 **示例代码(基于引用[^4]):** ```java // Java示例 private void setDrawableSize(TextView textView, int drawableResId, int widthPx, int heightPx) { Drawable drawable = getResources().getDrawable(drawableResId); drawable.setBounds(0, 0, widthPx, heightPx); // 设置宽度和高度(像素值) textView.setCompoundDrawables(null, null, drawable, null); // 设置为右边drawable,其他方向类似 } // 在Activity中调用 TextView tv = findViewById(R.id.textView); setDrawableSize(tv, R.drawable.your_icon, 50, 50); // 例如设置50x50像素 ``` 或Kotlin版本: ```kotlin // Kotlin扩展函数简化 fun TextView.setDrawableSize(drawableResId: Int, widthPx: Int, heightPx: Int) { val drawable = ContextCompat.getDrawable(context, drawableResId)?.apply { setBounds(0, 0, widthPx, heightPx) } setCompoundDrawables(null, null, drawable, null) } // 使用 findViewById<TextView>(R.id.textView).setDrawableSize(R.drawable.your_icon, 50, 50) ``` **优点:** 灵活,支持动态调整大小(如在适配不同屏幕时)。 **缺点:** 需要额外代码,硬编码像素值可能需要处理密度转换(dp转px)[^4]。 ### 注意事项 - **默认限制:** 直接使用android:drawableTop等属性只能控制padding,无法改变drawable本身大小[^1]。 - **尺寸单位:** 在代码中setBounds()使用像素(px),建议先通过`TypedValue.applyDimension()`或类似方法将dp转为px以适应不同屏幕密度,如引用[^5]所示。 - **其他方法:** 您可以使用自定义View或第三方库,但上述两种方法是官方推荐的核心解决方案。 ### 相关问题 1. 如何在XML中动态调整drawable大小以适应不同屏幕密度[^3]? 2. Android TextView设置drawable时如何处理位置和间距问题[^1]? 3. 代码中设置drawable大小时,如何确保在不同设备上显示一致[^5]? 4. 有哪些常见错误会导致TextViewdrawable不显示[^4]? 5. 除了drawable大小,如何改变TextViewdrawable的颜色或样式[^2]?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值