我有一张尺寸为6x10 dp的图片
我想将其动态添加到textView。
我有3个文本视图,并根据某种状态将其添加为compoundDrawable。
TextViewCompat.setCompoundDrawablesRelative(targetTextView, null, null, drawable, null);
我希望图像几乎垂直于文本视图居中+比中心低1 sp长度。所以我在每个文本视图中添加了
android:gravity="center_vertical"
如此:
我试图
1)创建一个高度为8 x 10的画布并将其居中放置在文本视图中,然后将抽屉绑定到(0,1sp,10,8-1sp)
但是,似乎在添加完整的imageView之前,文本视图相对于其位置也向下移动。
2)创建一个高度为textview的画布并将其居中放置在文本视图中,然后将抽屉绑定到(0,(textView height-6)/ 2,10,...)
但是随后等待observerTree使代码繁琐。
还有其他方法吗?
这是我的(1)选项的代码:
private void tintAndAddPadding(
...
@DrawableRes int drawableId, @ColorRes int colorId) {
Drawable drawable =
Preconditions.checkNotNull(AppCompatResources.getDrawable(getContext(), drawableId));
Drawable drawableWithTint = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(
drawableWithTint.mutate(), getContext().getResources().getColor(colorId));
// Bitmap dimensions sets the canvas dimensions
Bitmap bitmap =
Bitmap.createBitmap(
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.account_menu_collapse_expand_width),
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.account_menu_collapse_expand_canvas_height),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// Drawable bounds should be all 4 coordinates on the canvas to draw on.
// This implies the drawable width and height.
drawableWithTint.setBounds(
0,
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.account_menu_collapse_expand_top_padding)
, canvas.getWidth(),
canvas.getHeight() -
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.account_menu_collapse_expand_top_padding));
drawableWithTint.draw(canvas);
TextViewCompat.setCompoundDrawablesRelative(targetTextView, null, null, drawableWithTint, null);
// Padding between text and drawable
targetTextView.setCompoundDrawablePadding(
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.account_menu_collapse_expand_left_padding))
}