<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="@dimen/padding_small_6"
android:includeFontPadding="false"
android:text="@string/forgot_password_hint"
android:drawableStart="@drawable/ic_edit"
android:textSize="@dimen/text_size_medium_14"/>
这是我的textview。我想在此textview的左侧设置drawable。但是问题是,如果文本为多行,则drawable在文本视图中显示为垂直居中。
是否有任何选项可以在textview的左上角设置可绘制?
答案 0 :(得分:1)
否,没有任何直接选项可以在textview的左上角设置可绘制。
但是您可以创建一些自定义设计来实现此目的。将您的TextView
替换为以下代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_edit" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="@string/forgot_password_hint"
android:textSize="@dimen/text_size_medium_14"/>
</LinearLayout>
答案 1 :(得分:1)
通过扩展可绘制类来制作自定义可绘制类,并将其用于可绘制.eg:
public class TopLeftGravityDrawable extends BitmapDrawable {
private final Drawable mDrawable;
public TopLeftGravityDrawable(Drawable drawable) {
mDrawable = drawable;
}
@Override
public int getIntrinsicWidth() {
return mDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mDrawable.getIntrinsicHeight();
}
@Override
public void draw(Canvas canvas) {
int halfCanvas= canvas.getHeight() / 2;
int halfDrawable = mDrawable.getIntrinsicHeight() / 2;
// align to top
canvas.save();
canvas.translate(0, -halfCanvas + halfDrawable);
mDrawable.draw(canvas);
canvas.restore();
}
}
用法:
Drawable drawable = getResources().getDrawable(R.drawable.ic_customer);
TopGravityDrawable gravityDrawable = new TopGravityDrawable(drawable);
drawable.setBounds(0, 6, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
gravityDrawable.setBounds(0, 6, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
textview.setCompoundDrawables(gravityDrawable, null, null, null);
答案 2 :(得分:0)
如果适合采用编程方法,则可以扩展Drawable,使其占据可用的全部高度并在顶部绘制。
如果只想使用界面生成器作为解决方案,则应将TextView包裹在水平LinearLayout中,添加ImageView并将ImageViews的引力设置为TOP。
答案 3 :(得分:0)
在xml文件中尝试
将android:drawableStart更改为android:drawableLeft
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="@dimen/padding_small_6"
android:includeFontPadding="false"
android:text="@string/forgot_password_hint"
android:drawableLeft="@drawable/ic_edit"
android:textSize="@dimen/text_size_medium_14"/>
答案 4 :(得分:0)
使用android:paddingBottom="10dp"
答案 5 :(得分:0)
这解决了我的问题:
class MyTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null
) : AppCompatTextView(context, style) {
private val leftDrawable = ContextCompat.getDrawable(context, R.drawable.checkmark)
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
setBulletPoint(compoundDrawables[0], canvas)
}
private fun setBulletPoint(drawableLeft: Drawable?, canvas: Canvas?) {
if (!TextUtils.isEmpty(text)) {
leftDrawable?.let { drlft ->
if (lineCount == 1) {
setCompoundDrawablesWithIntrinsicBounds(drlft, null, null, null)
} else {
val buttonWidth = drlft.intrinsicWidth
val buttonHeight = drlft.intrinsicHeight
val topSpace = abs(buttonHeight - lineHeight) / 2
drlft.setBounds(0, topSpace, buttonWidth, topSpace + buttonHeight)
canvas?.apply {
save()
drlft.draw(canvas)
restore()
}
}
}
}
}
}