是否可以在文本视图中缩放左边和右边的图形?

我有 TextViewdrawableLeftdrawableRight在清单项目。 问题是,当 TextView的高度较大时,drawableLeftdrawableLeft不会根据 TextView的高度自动缩放。

有没有可能在 TextView中调整 drawableLeftdrawableRight的高度? (我使用的是9个补丁图像)

textview drawableleft & drawableright's height problem

104083 次浏览

我没有找到方法。但是你展示的看起来像是一个项目列表。每个项目将是一个 LinearLayout 水平项,由左至右为 images 视图,中间为文本。图像维度执行一个 android 操作: scaleType = “ fitXY”以调整大小高度到 textview 高度。 如果你不希望图像变形,可以使用 scaleType = “ CENTER _ INSIDE”。 < a href = “ http://developer.android.com/reference/android/widget/ImageView.ScaleType.html”rel = “ nofollow”> http://developer.android.com/reference/android/widget/imageview

<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>

只要让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);
}
});

这个也许能帮到你。 有两个属性 缩放规模

下面的代码将缩小图像和文本的30% 。 因此,你必须增加字体大小与许多“ sp”,以便当它得到重新调整大小(缩放) ,它将适合“ sp”您喜欢。

例子。如果我将字体设置为18,那么18中的30% 是5.4 sp,所以粗略地说,这就是我的目标值,因为当它缩放时,它将变成13 sp

<TextView
android:textSize="18sp"
android:scaleX="0.7"
android:scaleY="0.7"

最后要做的事情是设置 ComundDrawable。

tview.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.xxx), null, null, null);

希望这个能帮上忙

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
);

这里唯一可以接受的答案应该是像往常一样使用带有 scaleType 的 ImageView。在 Android 不支持的 TextView 上缩放图片似乎是一件很奇怪的事情。.没必要。按照预期使用 SDK。

我认为最干净的解决方案是覆盖 TextView:

Https://gist.github.com/hrules6872/578b52fe90c30c7445a2

我变了一点:

private void resizeCompoundDrawables() {
Drawable[] drawables = getCompoundDrawables();
if (compoundDrawableWidth > 0 || compoundDrawableHeight > 0) {
for (Drawable drawable : drawables) {
if (drawable == null) continue;


Rect realBounds = drawable.getBounds();


float drawableWidth = realBounds.width();
if(this.compoundDrawableWidth>0)
drawableWidth = this.compoundDrawableWidth;
float drawableHeight = realBounds.height();
if(this.compoundDrawableHeight>0)
drawableHeight = this.compoundDrawableHeight;


realBounds.right = realBounds.left + Math.round(drawableWidth);
realBounds.bottom = realBounds.top + Math.round(drawableHeight);


drawable.setBounds(realBounds);
}
}
super.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}

这样你可以设置可绘制的大小,但如果你关心宽高比,使它的方式适合你。

现在,如果你想让它自动匹配文本视图的高度,你也可以覆盖 TextView的方法 onSizeChanged:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
compoundDrawableHeight = h;
resizeCompoundDrawables();
}

你我也想为每一边声明大小,例如 left CompoundDrawableWidth,right CompoundDrawableWidth 等等。

请注意,它不能像绘图一样与 谢普一起工作。听起来像 TextView只接受它,如果它有大小属性,这样一个人可以实现它通过更新大小的可塑性使用 setSize 或 setintrinsicWidth 或... 未测试。

也许值得看看 ImageSpan。它是一个跨度,用一个可绘制的文本替换它所附加的文本,这个文本可以与底部对齐,也可以与周围的基线对齐。我认为它也会根据文本视图的高度自动缩放。

Https://developer.android.com/reference/android/text/style/imagespan

另一种为 TextView 内部的可绘图件创建自定义大小的方法是使用 BindingAdapter。使用它,您将能够在 xml 中设置图像大小。

例如,TextViewBindingAdapter.java

public class TextViewBindingAdapter {
@BindingAdapter({"android:imageSize"})
public static void bindIconSize(TextView textView, int size) {
Drawable[] drawables = textView.getCompoundDrawables();
if(drawables[0] != null) {
drawables[0].setBounds(0, 0, size, size); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
textView.setCompoundDrawables(
drawables[0], //left
null, //top
null, //right
null //bottom
);
}
if(drawables[1] != null) {
drawables[1].setBounds(0, 0, size, size); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
textView.setCompoundDrawables(
null, //left
drawables[1], //top
null, //right
null //bottom
);
}
if(drawables[2] != null) {
drawables[2].setBounds(0, 0, size, size); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
textView.setCompoundDrawables(
null, //left
null, //top
drawables[2], //right
null //bottom
);
}
if(drawables[3] != null) {
drawables[3].setBounds(0, 0, size, size); // setBounds(int left, int top, int right, int bottom), in this case, drawable is a square image
textView.setCompoundDrawables(
null, //left
null, //top
null, //right
drawables[3] //bottom
);
}




}
}

现在,您可以在.xml 中设置 android: imageSize。

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:drawableLeft="@drawable/alpha_s_circle"
android:drawablePadding="4dp"
android:gravity="center_vertical"
android:textSize="16sp"
android:imageSize="@{300}"/>

不要忘记设置你的年级。建立以下代码:

android {
....
buildFeatures {
dataBinding = true
}
}

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

<?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 >

然后,在 textview 标记中使用这个绘图工具

我尝试了所有的解决方案,但没有一个对我有用,然后我试了这个

TextView

           <TextView
android:gravity="left"
android:id="@+id/product_view_count"
android:text="@string/view_product_count"
android:drawableLeft="@drawable/ic_eye"
android:drawablePadding="2dp"
android:drawableTint="@color/orange"
android:textSize="20sp"
android:textColor="@color/orange"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

在您的活动或片段或适配器。我使用我的适配器像这样

viewcount=itemView.findViewById(R.id.product_view_count);


holder.viewcount.setPivotX(0);
holder.viewcount.setPivotY(0);
holder.viewcount.setScaleX(.7f);
holder.viewcount.setScaleY(.7f);

通过使用 ScaleX, ScaleYTextView文本位置将改变。保持位置在 left/start使用 setPivotX,setPivotY

感谢 Bö macht Blau的良好解决方案 给你

最简单的方法是创建一个新的可绘制对象并调整可绘制对象的大小

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
**android:width="50dp"
android:height="50dp"**
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@android:color/white">
<path
android:fillColor="@android:color/black"
android:pathData="M19,3H4.99C3.89,3 3,3.9 3,5l0.01,14c0,1.1 0.89,2 1.99,2h10l6,-6V5C21,3.9 20.1,3 19,3zM7,8h10v2H7V8zM12,14H7v-2h5V14zM14,19.5V14h5.5L14,19.5z"/>
</vector>