android - android 中的可绘制文本与左对齐

标签 android android-edittext android-canvas android-view textview

我是自定义 View 的新手,对android中的canvas不太了解, 我想将左侧可绘制对象与布局的右侧以及相同 TextView 的文本(无论是否多行)对齐,如图所示 enter image description here

在上图中,我想将 $ 图像与文本对齐($20.0 - $90.0/小时),并且如果文本是多行并且我不想使用任何额外的布局,则必须居中。提前致谢

这是我的基本 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>

我曾尝试使用自定义 View 来实现它,它适用于小文本,但是当文本变大时,它会变得很奇怪,我的代码图像如下,enter image description here

最佳答案

尝试这样的事情

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 - android 中的可绘制文本与左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912539/

相关文章:

android - Android 中如何验证两个签名或抽奖是否相似?

android - alertdialog ListView 取消选中

android - 防止未经授权的人卸载我的 Android 应用程序

java - 添加库后无法合并dex错误

android - Titanium USB 热敏打印机驱动程序

java - Android从其他线程获取EditText内容

java - 我得到 java.lang.StackOverflowError : stack size 8MB when i draw my canvas

android格式edittext每4个字符后显示空格

android - EditText语言支持?

java - 每次我尝试更改油漆的属性(厚度或颜色)时,它都会更改 Canvas 上已有的现有油漆(android)(java)