我有物品清单是TextView
;并使用drawableleft & drawableright来证明TextView
。 问题是,只要textview中的文字较大,drawableleft & drawableleft不会根据TextView
的高度自动缩放。是否可以在textview中缩放drawableleft&drawableright?
是否可以在textview中缩放drawableleft的高度& drawableright? (我用的是9修补图像)
我有物品清单是TextView
;并使用drawableleft & drawableright来证明TextView
。 问题是,只要textview中的文字较大,drawableleft & drawableleft不会根据TextView
的高度自动缩放。是否可以在textview中缩放drawableleft&drawableright?
是否可以在textview中缩放drawableleft的高度& drawableright? (我用的是9修补图像)
我没有找到办法。但是你看起来像一个项目列表。每个项目都是一个LinearLayout水平,从左到右的图像视图和中心的文本。对于尺寸高度为textview高度的图片尺寸,执行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>
我已经成功地使用了这个解决方案,因为我无法缩放左/右绘图,因为问题建议 – Elemental 2013-02-05 13:41:25
只是让9路径的背景重复这种模式。
也似乎它会更好看看的情况下,模式将被应用于列表,而不是单个项目。
我通过引入ScaleDrawable
并覆盖它的.getIntrisicHeight()
来解决等效用例,因此它至少是TextView
高度。该TextView.addOnLayoutChangeListener
部分,重新绑定在TextView
尺寸变化Drawable
需要与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);
}
});
好的答案,好的代码格式。 – 2016-05-21 09:36:56
请有此代码的尝试。这可能会帮助你。
Drawable TextViewDrawable = context.getResources().getDrawable(imageId);
TextViewDrawable.setBounds(0, 0, TextViewDrawable.getIntrinsicWidth(),
TextViewDrawable.getIntrinsicHeight());
text_view.setCompoundDrawables(left, top, right, bottom);
这甚至不会编译... – katzenhut 2015-04-27 13:40:16
不行,不起作用。 – 2016-10-15 14:47:24
这可能会帮助你。 有两个属性的scaleX和的scaleY
下面的代码会按比例缩小图片,并用30%的文字。 因此,你必须增加字体大小,以便当它重新调整大小(缩放)时,它将适合你喜欢的“sp”。
例子。如果我设置字体为18,则30%了18的是5.4sp,所以大概,这就是我的目标价值,因为当它被缩小,这将成为13SP
<TextView
android:textSize="18sp"
android:scaleX="0.7"
android:scaleY="0.7"
的最后一件事要做的是设置CompundDrawable。
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
);
使用此链接:[编辑文本化合物可绘制(http://stackoverflow.com/questions/5471026/edittext-with-non-selectable-grayed-out -prefix) – 2011-03-30 08:44:04
您可以修改textview的以下内容。此外,可绘制的setCompound参数有可绘制的右边的选项,可绘制的left.void android.widget。 TextView中。setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom) – 2011-03-30 08:45:47
thanks :) 但它不起作用,图片仍然无法伸展。我尝试使用textview height(textview.getHeight())设置可绘制边界,但是它返回文本视图设计高度而不是当前高度。 – hakim 2011-03-30 10:44:22