是否可以在textview中缩放drawableleft和drawableright?

hak*_*kim 52 android textview

我有项目列表是一个TextView; 并使用drawableleft&drawableright来塑造drawableLeft.问题是,每当textview中的文本较大时,drawableleft和drawableleft都不会根据drawableRight高度自动缩放.

是否可以在textview中缩放drawableleft和drawableright的高度?(我使用9补丁图片)

textview drawableleft&drawableright的高度问题

Sha*_*ahi 31

将您的资源包装在一个可绘制的文件中,该可绘制文件定义了您所需的大小,类似于:

<?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>
Run Code Online (Sandbox Code Playgroud)

之后,在你的textview标签中使用这个drawable

  • 这是最简洁的答案,允许您将所有布局代码保留在 XML 中,而无需引入多个视图(ImageView + TextView)。谢谢! (5认同)
  • 仅适用于 api 23 及更高版本 (4认同)

kir*_*ilv 26

这可能会帮助你.scaleXscaleY有两个属性

下面的代码将缩小图像和文本的30%.因此,您必须使用许多"sp"增加字体大小,以便在重新调整大小(缩放)时,它将适合您喜欢的"sp".

例.如果我将字体设置为18,那么18%中的30%是5.4sp,粗略地说,这是我所针对的值,因为当它被缩放时,它将变为13sp

<TextView
        android:textSize="18sp"
        android:scaleX="0.7"
        android:scaleY="0.7"
Run Code Online (Sandbox Code Playgroud)

最后要做的是设置CompundDrawable.

tview.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.xxx), null, null, null);
Run Code Online (Sandbox Code Playgroud)

  • 我肯定不是最优雅的,但我已经尝试了很多东西,而且这太容易让人问这么简单.谢谢! (3认同)
  • 智能解决方案,易于实施 (2认同)
  • 不知道为什么人们会说这是一个很好的解决方案...这是一个非常不可扩展且不雅致的解决方案,因为您需要弄乱sp值才能找到可行的方法。请参阅我的回答,应按原样使用ImageViews。TextView仅具有一个drawable属性,以易于使用,它无法涵盖您想要对图像进行缩放等每个用例。按预期使用SDK。 (2认同)
  • 如果您只想缩放图像怎么办? (2认同)

bla*_*lle 9

我通过引入ScaleDrawable并覆盖它来解决一个等效的用例.getIntrisicHeight(),使其至少达到TextView高度.的TextView.addOnLayoutChangeListener部分,重新绑定需要DrawableTextView尺寸变化可与API11 +

Drawable underlyingDrawable = 
        new BitmapDrawable(context.getResources(), result);

// Wrap to scale up to the TextView height
final ScaleDrawable scaledLeft = 
        new ScaleDrawable(underlyingDrawable, Gravity.CENTER, 1F, 1F) {
    // Give this drawable a height being at
    // least the TextView height. It will be
    // used by
    // TextView.setCompoundDrawablesWithIntrinsicBounds
    public int getIntrinsicHeight() {
        return Math.max(super.getIntrinsicHeight(), 
                        competitorView.getHeight());
    };
};

// Set explicitly level else the default value
// (0) will prevent .draw to effectively draw
// the underlying Drawable
scaledLeft.setLevel(10000);

// Set the drawable as a component of the
// TextView
competitorView.setCompoundDrawablesWithIntrinsicBounds(
        scaledLeft, null, null, null);

// If the text is changed, we need to
// re-register the Drawable to recompute the
// bounds given the new TextView height
competitorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

    @Override
    public void onLayoutChange(View v, int left, int top, int right, 
            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        competitorView.setCompoundDrawablesWithIntrinsicBounds(scaledLeft, null, null, null);
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 8

希望这有帮助

Textview tv = (TextView) findViewById(R.id.tv_dummy)
int imageResource =  R.mipmap.ic_image;
Drawable drawable = ContextCompat.getDrawable(context, imageResource);
int pixelDrawableSize = (int)Math.round(tv.getLineHeight() * 0.7); // Or the percentage you like (0.8, 0.9, etc.)
drawable.setBounds(0, 0, pixelDrawableSize, pixelDrawableSize); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
tv.setCompoundDrawables(
 null, //left
 null, //top
 drawable, //right
 null //bottom
);
Run Code Online (Sandbox Code Playgroud)


mpe*_*egr 5

唯一可以接受的答案应该是照常使用带有scaleTypes的ImageView。hacky解决方法可以在Android不支持的TextView上缩放图像。按预期使用SDK。

  • 将 Textview 和 ImageView 包装在 LinearLayout 中会生成有关使用CompoundDrawables 的通知。我想问题在于是否按照编写 API 的人的“预期”或“推荐”使用该 API。 (3认同)

Zeu*_*ics 3

我没有找到办法。但您显示的内容看起来像是一个项目列表。每个项目都是一个水平的 LinearLayout,imagesView 从左到右,文本在中心。图像尺寸执行 android:scaleType="fitXY" 将尺寸高度调整为文本视图高度。如果您不想使图像变形,请使用scaleType =“CENTER_INSIDE”。http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

<LinearLayout
     android:layout_width="fill_parent"
     android:id="@+id/HeaderList" 
     android:layout_gravity="top"
     android:layout_height="wrap_content" >  


     <ImageView
         android:id="@+id/backImg"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_centerInParent="true"
         android:adjustViewBounds="true"
         android:background="@color/blancotransless"
         android:src="@drawable/header"
         android:scaleType="fitXY" >
     </ImageView>

         <TextView 
             android:layout_height="wrap_content"
             android:layout_width="wrap_content" 
             android:id="@+id/NameText"
             android:text="The time of prayer" 
             android:textColor="#FFFFFF"
             android:textSize="30sp" 
             android:layout_alignParentLeft="true"
             android:layout_alignParentTop="true" 
             android:paddingLeft="4dp"
             android:paddingTop="4dp" 
             />
    <ImageView
         android:id="@+id/backImg"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_centerInParent="true"
         android:adjustViewBounds="true"
         android:background="@color/blancotransless"
         android:src="@drawable/header"
         android:scaleType="fitXY" >
     </ImageView>

</LinearLayout> 
Run Code Online (Sandbox Code Playgroud)