left drawable alignment with text in android

这一生的挚爱 提交于 2019-12-02 01:15:19

try something like this

package views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

import com.upsilon.docta.R;


public class MyTempView extends TextView {
    public MyTempView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
public MyTempView(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    //ContextCompat.getColor(mContext, R.color.back_text_color)
    Paint textPaint = getPaint();
    Rect bounds = new Rect();
    textPaint.getTextBounds(getText().toString(), 0, getText().length(), bounds);
    int textWidth = bounds.width();
    Resources res = getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_close);
    canvas.drawBitmap(bitmap, 0, getHeight() / 2 - bitmap.getHeight() / 2, textPaint);
    canvas.translate(bitmap.getWidth(), 0);
    super.onDraw(canvas);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/ad_post_tv_noDays"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:drawableLeft="@drawable/ic_cart_final"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="20 days"
    android:textColor="@color/blue_btn_bg_color" />

<TextView
    android:id="@+id/ad_post_tv_wage"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:drawableLeft="@drawable/ic_cart_final"
    android:drawablePadding="5dp"
    android:layout_gravity="right"
    android:gravity="center_vertical|right"
    android:text="$8 - $12/hour"
    android:textSize="10dp"
    android:textStyle="bold" /></LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!