在Andorid的文本左对齐绘制 [英] left drawable alignment with text in andorid

查看:183
本文介绍了在Andorid的文本左对齐绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的自定义视图,也不太了解android的帆布,
我想与文本一起对准左绘制布局的右侧(无论是多行与否),因为这形象同TextView中的的

I am new to custom view and didn't know much about canvas in android, I wanted to align a left drawable to right side of layout along with the text(whether multiline or not) of same textview as in this image

在上面的图片,我想与文本一起对准$图像($ 20.0 - $为90.0 /小时),并且必须是中心,如果文字是多行,我不希望使用任何额外的布局。在此先感谢

In the above image, i wanted to align $ image along with text($20.0 -$90.0 /hour) and must be in center if text is multiline and i don't want to use any extra layouts. Thanks in advance

这是我的基本的XML,

This is my basic xml,

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"">

        <TextView
            android:id="@+id/ad_post_tv_noDays"
            style="@style/textView"
            android:text="20 days"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:drawablePadding="5dp"
            android:textColor="@color/color60"
            android:drawableLeft="@drawable/icon_calender"
            android:layout_gravity="left|center"
            android:gravity="left|center"
            android:layout_centerVertical="true"
            />

        <TextView
            style="@style/textView"
             android:layout_alignParentRight="true"
            android:id="@+id/ad_post_tv_wage"
            android:layout_marginLeft="5dp"
            android:text="$8 - $12/hour"
            android:textSize="10dp"
            android:layout_toRightOf="@id/ad_post_tv_noDays"
            android:drawablePadding="5dp"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:layout_gravity="right|center"
            android:gravity="right|center"
            android:drawableLeft="@drawable/icon_dollar"
            />
    </RelativeLayout>

我试过用自定义视图来实现它,它适用于小文,但是当文本变得更大,这样会很奇怪,我的code图像如下,的

推荐答案

尝试这样的事情

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

这篇关于在Andorid的文本左对齐绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆