在Android中与文本左可绘制对齐 [英] left drawable alignment with text in android

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

问题描述

我是自定义视图的新手,对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/hour),并且如果文本为多行并且我不想使用任何额外的布局,则必须居中.预先感谢

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,

 <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>

我尝试用自定义视图实现它,它适用于小文本,但是当文本变大时,它变得怪异,我的代码图像如下,

I had tried to implement it with custom views, it works for small text but when the text got bigger,it gets weird, my code image is as follows,

推荐答案

尝试类似的方法

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

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

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