简体   繁体   English

在 TextView android 中调整可绘制图像的大小

[英]Resize drawable image in TextView android

Wonder how to resize an image inside a TextView xml in a LinearLayout .想知道如何在LinearLayoutTextView xml 中调整图像大小。 I read some solution using setCompoundDrawablesWithIntrinsicBounds method, but cannot get it right.我阅读了一些使用setCompoundDrawablesWithIntrinsicBounds方法的解决方案,但无法正确解决。

Here is the image:这是图像:

在此处输入图像描述

I want to reduce the size of the logo.我想减小徽标的大小。

Here is my TextView code:这是我的 TextView 代码:

<TextView  
    android:id="@+id/heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/android_logo"
    android:drawableStart="@drawable/android_logo"
    android:gravity="top"
    android:padding="16dp"
    android:singleLine="true"
    android:text="@string/heading"
    android:textSize="20dp"
    android:drawablePadding="5dp"/>

Try:尝试:

 android:scaleX="0.7"
 android:scaleY="0.7"

OR要么

Wrap your resource in a drawable that defines your desired size similar to:将您的资源包装在定义所需大小的可绘制对象中,类似于:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item
      android:drawable="@drawable/icon"
      android:width="@dimen/icon_size"
      android:height="@dimen/icon_size"
      />

</layer-list >

then use it in drawable left.然后在 drawable left 中使用它。

there is only one solution take image view for the image and resize image but text display image right side.只有一种解决方案为图像获取图像视图并调整图像大小,但文本显示图像右侧。 you used below code and i hope your issue solved..你使用了下面的代码,我希望你的问题解决了..

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_person" />

<TextView
    android:id="@+id/heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:drawablePadding="5dp"
    android:gravity="top"
    android:padding="5dp"
    android:singleLine="true"
    android:text="heading"
    android:textSize="20dp"
    app:layout_constraintLeft_toRightOf="@+id/icon" />

Programmatic solution:程序化解决方案:

You can use drawable.setBounds but it requires pixels rather than dp as parameters.您可以使用drawable.setBounds但它需要像素而不是 dp 作为参数。

how to convert: Converting pixels to dp如何转换: 将像素转换为dp

code:代码:

        Drawable drawable = getDrawable(R.drawable.ic_your_drawable);
        int size = dp2px(this, dpSize);
        if (drawable != null) {
            drawable.setBounds(0, 0, size, size);
        }

        textView.setCompoundDrawables(drawable, drawable, drawable, drawable); // should be (drawable, null, null, null) for you 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM